|
Well that took me awhile to understand and figure out. Guess I should of asked how to use it.
Anyways thanks Richard!
If it ain't broke don't fix it
|
|
|
|
|
What is the best practice in .NET with Web API? Especially Web REST API. Should a REST API return exception in response body when an exception happens?
For sure I will return a 500 or similar HTTP status. But when I response with this error code what are the best practice? Or even better what is the specification or REST API about this?
- return the exception (what I do)
- return en empty response body?
- return an empty of default JSON object?
- something else?
FYI I'm working on a Web pragmatic REST API in C# .NET that is consumed by a Ruby on Rails team and I'm not sure Ruby code can handle these exception message easily.
|
|
|
|
|
WEB API should return status code in the header and any content in the body.
For example, if the request is successful then set the status code to 200 in the header and send the result in the body.
If the request is failed , then set the corresponding status code in the header ( for example 500 etc...) and send additional information in the body.
|
|
|
|
|
Please I am working with MVC C#. I have a html table that is being populated from a database Table and its working fine, but my question is i want to save that same table to another database table. Please can anyone help me as its urgent. Thanks
AOD
|
|
|
|
|
Please do not post duplicate contents.
Also, do you need to save save html markup here to database? or contents you generated inside html table.
modified 20-Sep-20 21:01pm.
|
|
|
|
|
You are not clearly explaining yourself. What is your issue?
|
|
|
|
|
I don't know if I am in the right place to ask a question in reference to using a variable inside a view. I want the variable to go inside the @Html.EditorFor(model => variable). Any help appreciated. Thanks.
UPDATE:
I think I have solved my problem. My application is used to store internet sites user/password information. The data is stored in data/value pairs. The value is encrypted with my own code. I needed to run the value item through the encryption code so it could be stored in the database. I created another object with data from the database, and compared the two value objects to see if it should be saved. If so the value item was run through the encrypt code. Then the second object was unattached and the edited object saved. Thanks for the help.
modified 19-Jun-17 14:48pm.
|
|
|
|
|
You can have that variable as part of your your view model so you can use it on view. Assuming you have requirement to use it as html helper only per your code you showing in description.
modified 20-Sep-20 21:01pm.
|
|
|
|
|
Vinod,
UPDATE:
I think I have solved my problem. My application is used to store internet sites user/password information. The data is stored in data/value pairs. The value is encrypted with my own code. I needed to run the value item through the encryption code so it could be stored in the database. I created another object with data from the database, and compared the two value objects to see if it should be saved. If so the value item was run through the encrypt code. Then the second object was unattached and the edited object saved. Thanks for the help.
|
|
|
|
|
What do you want to do with the variable ? It can be part of the model. Post a sample code for what you are trying to do.
|
|
|
|
|
John,
UPDATE:
I think I have solved my problem. My application is used to store internet sites user/password information. The data is stored in data/value pairs. The value is encrypted with my own code. I needed to run the value item through the encryption code so it could be stored in the database. I created another object with data from the database, and compared the two value objects to see if it should be saved. If so the value item was run through the encrypt code. Then the second object was unattached and the edited object saved. Thanks for the help.
|
|
|
|
|
I am VERY new to Asp.Net, so bear with my ignorance.
I added an Export button to a page:
<input type="button" value="Export" onclick="location.href='@Url.Action("Export", "Home")'" />
In the controller I added
public ActionResult Export()
{
var data = _dal.GetDashboardInfos(new DashboardInfoQueryArgs());
var d = data.ToList().ToCsv();
var path = Path.GetTempPath();
var fileData = string.Format("{0}ardexport.csv", path);
using (StreamWriter sw = new StreamWriter(fileData, false))
{
sw.WriteLine(d);
}
Process.Start(fileData);
return View("Index", "Home");
}
The CSV open fine, then I get a page load error:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
.
.
.
After the export I would really like to NOT reload (no reason to), or, at the very least just reload the default page.
What I doing wrong here?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Kevin Marois wrote: Process.Start(fileData);
That's not doing what you think it's doing.
The code is writing to a file on the server, and then attempting to open the file on the server. It appears to work when you run it from Visual Studio, but that's just because the server and the client happen to be the same computer in that instance.
As soon as you deploy your code to a real server, you'll either get an exception indicating that there's no application associated with the file type; or the file will open on the server, where nobody will ever see it.
If you want to send the data back to the client, you'll need to send it via a FileResult :
public ActionResult Export()
{
var data = _dal.GetDashboardInfos(new DashboardInfoQueryArgs());
string content = data.ToList().ToCsv();
byte[] bytes = Encoding.UTF8.GetBytes(content);
var result = new FileContentResult(bytes, "text/plain");
result.FileDownloadName = "ardexport.csv";
return result;
}
Unfortunately, this means you can't return a file and redirect. This is a limitation of how the web works - a single request can only generate a single response.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you. Good thing I posted.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I don't really want to redirect.. I just didn't want it to try to load a view called 'export'. I wanted it to stay on the page it's on.
The code you posted seems to work just fine.
Thank you!
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Hi all,
I am little new to MVC and WebApi, I am here trying to post values of html form to a HttpPost method of Web Api, here how I am using it
$("#btnSave").click(function () {
var defaultText = $("#defaultText").val();
var txtLanId = $("#txtLanId").val();
var txtLast = $("#txtLast").val();
var txtFirst = $("#txtFirst").val();
var txtEmployeeId = $("#txtEmployeeId").val();
var txtEmail = $("#txtEmail").val();
var dtFieldHireDate = $("#dtFieldHireDate").val();
var dtFieldRehireDate = $("#dtFieldRehireDate").val();
var lbxApplicationGroup = $("#lbxApplicationGroup").val();
var lbxReportPack = $("#lbxReportPack").val();
alert(dtFieldHireDate);
alert(dtFieldRehireDate);
alert(lbxApplicationGroup);
alert(lbxReportPack);
$.ajax({
url: "/api/EmployeeAPI",
type: "Post",
data: JSON.stringify([defaultText, txtLanId, txtLast, txtFirst, txtEmployeeId, txtEmail, dtFieldHireDate, dtFieldRehireDate, lbxApplicationGroup, lbxReportPack]),
contentType: 'application/json; charset=utf-8',
success: function (data) { },
error: function () { alert('error'); }
});
});
When I am trying to retrieve those values in the Web Api method, I am getting empty strings, I am not sure what's going wrong here, but any help is going to be very helpful, thanks in advance.
[System.Web.Http.HttpPost]
public string PostAction(System.Net.Http.Formatting.FormDataCollection form)
{
string retValues;
retValues = form.Get("txtLast") + form.Get("txtLanId");
return retValues;
}
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
The FormDataCollection expects an x-www-form-urlencoded submission. You're submitting a JSON-serialized array of strings.
If you don't want to change the API method, you'll need to change the script that calls it:
$("#btnSave").click(function () {
var postData = {
defaultText = $("#defaultText").val(),
txtLanId = $("#txtLanId").val(),
txtLast = $("#txtLast").val(),
txtFirst = $("#txtFirst").val(),
txtEmployeeId = $("#txtEmployeeId").val(),
txtEmail = $("#txtEmail").val(),
dtFieldHireDate = $("#dtFieldHireDate").val(),
dtFieldRehireDate = $("#dtFieldRehireDate").val(),
lbxApplicationGroup = $("#lbxApplicationGroup").val(),
lbxReportPack = $("#lbxReportPack").val()
};
console.log("POST", postData);
$.ajax({
type: "POST",
url: "/api/EmployeeAPI",
data: postData,
success: function(data) { console.log("Success", data); },
error: function() { alert("Error"); }
});
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I did this but still the values are not being posted at the Post Method of Web Api, do I need to make any change in the Web Api method as well? Please let me know please my friend?
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Check the "network" tab in the browser's developer tools to see what's actually being sent to the server.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
how to use querry string with various methods
|
|
|
|
|
Depends on the methods. Perhaps you could edit your question and explain exactly what you are trying to do.
|
|
|
|
|
HttpRequest.QueryString Property (System.Web)[^]
There are two kinds of people in the world: those who can extrapolate from incomplete data.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
If you look at the Request object in your method (Of course it is a web application) , you will be able to see the QueryString property there and you can retrieve the values from it.
|
|
|
|
|
I need to be able to lock down the URL of a small application so that the client cannot use the URL with multiple devices. I need to be able to assign some type of generated key/license so that if the client purchases 1 URL they can only use that URL on 1 device. This has nothing to do with sessions and or restricting an employee by the device.
Can something like this be done? Can a unique key be added at the end of the URL and save into the device cookies or something like that
Any directions?
|
|
|
|
|
Unique keys can be added to URLs, can be sent via headers, can be stored on the server-side so that when a request comes by the URL is served otherwise a redirect is issued.
There are several ways at doing this, how do you want to do that, is a bit confusing. The thing is, your requirement needs a license, or a process that tells the system whether a user can access this information or not.
ASP.NET doesn't care about purchase systems, licensing, access etc. You have to manage that and tell ASP.NET, how to configure itself to allow/disallow access to certain parts of the application. That said, this is entirely possible, please be more specific or show some code so that we may guide you further.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|