400 bad request, but there is nothing wrong with this page - asp.net

I am using paged lists on my mvc application. I have tons of different pages that do this, but for SOME random reason, this one throws a 400 bad request error. I am almost positive the code is the same.
public ViewResult ManageWeapons(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "WeaponName_desc" : "";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var weapon = from s in db.Weapons
select s;
if (!String.IsNullOrEmpty(searchString))
{
weapon = weapon.Where(s => s.WeaponName.Contains(searchString));
}
switch (sortOrder)
{
case "WeaponName_desc":
weapon = weapon.OrderByDescending(s => s.WeaponName);
break;
default: // ascending
weapon = weapon.OrderBy(s => s.WeaponName);
break;
}
int pageSize = 25;
int pageNumber = (page ?? 1);
return View(weapon.ToPagedList(pageNumber, pageSize));
}
I invoke it as so, to sort a column
//This function updates a pagedList using Ajax
function getSorterWeapons(sortOrder, currentFilter) {
//alert($(this).val());
console.log("making it!");
$.ajax({
type: "GET",
url: '#Url.Action("ManageWeapons", "AdminTools")',
cache: false,
data: { sortOrder: sortOrder, currentFilter: currentFilter },
error: function () {
alert("An error occurred.");
},
success: function (data) {
$("#manageWeaponsBox").html(data);
console.log("success!");
alert(data);
}
});
}
I have set breakpoints on the server and it makes it through just fine. Nothing seems to be going wrong, but the client constantly outputs 400 error in console.
I am doing this EXACT same function for other tables in my database and it works fine. This one has me to wits end.

This one would have been rather obvious has it been in an HTML post. The problem is as simple as that I had an error in the view (from accidently adding a character). The c# function was executing fine and then the code was breaking in the view side, one of the pitfalls of debugging using ajax and mvc.

Related

Modify BuildApiResponse in ASP.Net Web Api

I am new in ASP.NET MVC Web API. I am trying to modified the return JSon to this format
{
"Error": false,
"Status": 200,
"Response": []
}
Now I able to do that by follow this post https://www.devtrends.co.uk/blog/wrapping-asp.net-web-api-responses-for-consistency-and-to-provide-additional-information . But the problem is I not able to show ModelState error like 'First name is required' because the code only show the first hit error.
if (error != null)
{
content = null;
//only show the first error
errorMessage = error.Message;
}
So I did some modification, now the code is written as below:
if (error != null)
{
content = null;
foreach(var e in error)
{
//if the error's type is ModelState
if (e.Key.Equals("ModelState"))
{
var allErrors = e.Value;
foreach (var modelError in (IEnumerable<KeyValuePair<string, object>>)allErrors)
{
var msg = modelError;
errorMessage = string.Concat(errorMessage, ", ", ((String[]) modelError.Value)[0]);
}
}
else
{
errorMessage = e.Value.ToString();
}
}
}
Now it's able to show all errors but the code is messy. I am writing this questions to find out what is the proper way to write this kind of code.
You can iterate over all the errors and concatenate them using StringBuilder. String.Join is much faster than Append for less than 1000 items (it is unlikely you will have so many errors in the modelstate object):
public static ValidationResult CheckValid(ModelStateDictionary modelState, string httpName = null)
{
if (!modelState.IsValid)
{
var sb = new StringBuilder();
sb.AppendLine(httpName + " failed: Invalid Json:");
foreach (var pair in modelState)
{
var error = String.Join(";", pair.Value.Errors.Select
(
i =>
{
if (!String.IsNullOrEmpty(i.ErrorMessage))
return i.ErrorMessage;
return i.Exception.Message;
}
));
sb.AppendLine($"Property: {pair.Key} Errors: ({error})");
}
return new ValidationResult(false, sb.ToString());
}
else
return new ValidationResult(true, "");
}

asp.net in OpenFileDialog

I am creating a web server through asp.net.
The source works well in debug mode.
However, after posting to iis, if you go through the source, you will get the error 'HTTP Error 503. The service is unavailable' after about 20 seconds.
I am confident that there is an error in the OpenFileDialog section.
In the past, this code worked well after posting. I do not know what has been modified since then.
Thanks in advance for your help.
.js code
action: function (e, dt, node, config) {
$.ajax({
"url": "/api/Member/ExcelRead",
"type": "POST",
"datatype": 'json',
success: function (data) {
if (data === 'OK') {
alert("성공");
}
},
error: function (response, state, errorCode) {
alert("실패");
}
});
.cs
public class MemberController : ApiController
{
[HttpPost]
public string ExcelRead()
{
ExcelHelper helper = new ExcelHelper();
Thread th = new Thread(helper.ReadExcelData);
th.SetApartmentState(ApartmentState.STA);
th.Start();
th.Join();
if (helper.data == null)
return ("NO");
return ("OK");
}
}
public void ReadExcelData()
{
IsRun = false;
OpenFileDialog openFile = new OpenFileDialog();
openFile.DefaultExt = "xlsx";
openFile.Filter = "Excel Files(*.xlsx)|*.xlsx";
DialogResult dresult = openFile.ShowDialog();
if (dresult != DialogResult.OK)
{
return;
}
if (openFile.FileNames.Length > 0)
{
foreach (string filename in openFile.FileNames)
{
//this.textBox1.Text = filename;
}
}
}

Post data with free-jqgrid, what are the code in client and Web API side?

I am using ASP.NET MVC 6 Web API. In the View I use free-jqgrid.
Let's borrow Oleg's free jqgrid data to demonstrate my purpose. We already have the table shown.
Next I am going to add new Vendor. Please notify that there is primary key id(identity column) in the database. We don't want it displaying in the screen.
In VendorRespository.cs, I add the new Vendor as
public void AddVendor(Vendor item)
{
using (VendorDataContext dataContext = new VendorDataContext())
{
dataContext.Database.Connection.ConnectionString = DBUtility.GetSharedConnectionString(
"http://centralized.admin.test.com");
var newVendor = dataContext.Vendors.Create();
newVendor.Company = item.Company;
newVendor.ContactName = item.ContactName;
newVendor.ContactPhone = item.ContactName;
newVendor.UserName = item.UserName;
newVendor.UserKey = item.UserKey;
newVendor.Active = item.Active;
newVendor.FacilityId =item.FacilityId;
newVendor.ClientID = item.ClientID;
dataContext.SaveChanges();
}
}
My questions:
Not sure the script like?
<script>
API_URL = "/VendorManagement/";
function updateDialog(action) {
return {
url: API_URL
, closeAfterAdd: true
, closeAfterEdit: true
, afterShowForm: function (formId) { }
, modal: true
, onclickSubmit: function (params) {
var list = $("#jqgrid");
var selectedRow = list.getGridParam("selrow");
rowData = list.getRowData(selectedRow);
params.url += rowData.Id;
params.mtype = action;
}
, width: "300"
};
}
jQuery("#jqgrid").jqGrid('navGrid',
{ add: true, edit: true, del: true },
updateDialog('PUT'),
updateDialog('POST'),
updateDialog('DELETE')
);
In the controller, not sure what is the code?
// POST
public HttpResponseMessage PostVendor(Vendor item)
{
_vendorRespository.AddVendor(item);
var response = Request.CreateResponse<Vendor>(HttpStatusCode.Created, item);
string uri = Url.Link("DefaultApi", new { id = item.Id });
response.Headers.Location = new Uri(uri);
return response;
}
My code has many compiling errors such as
'HttpRequest' does not contain a definition for 'CreateResponse' and the best extension method overload 'HttpRequestMessageExtensions.CreateResponse(HttpRequestMessage, HttpStatusCode, Vendor)' requires a receiver of type 'HttpRequestMessage'
Please help me to get rid of the error and inappropriate code.
EDIT:
I borrowed the code snippet from here.
I need add the code such as
[Microsoft.AspNet.Mvc.HttpGet]
public dynamic GetVendorById(int pkey)
{
return null;
}
And
// POST
[System.Web.Http.HttpPost]
public HttpResponseMessage PostVendor(Vendor item)
{
_vendorRespository.AddVendor(item);
var response = Request.CreateResponse<Vendor>(HttpStatusCode.Created, item);
string uri = Url.Link("/VendorManagement/GetVendorById", new { id = item.pkey });
response.Headers.Location = new Uri(uri);
return response;
}

selecting all the records from the database using jquery ajax in asp.net

i want to generate the table of contents from database.. using jquery ajax in asp.net, i am using sql server 2008 as a backend. for this i created a webmethod in my normal aspx page. and on the clientside wrote the ajax script to fetch records but when i loop through the results, i gets message undefined and nothing happens.. i want to generate table out of the records from database below is my webmethod.
[WebMethod]
public static Poll[] GetPollDetailed()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("sp_SelectQuestion", con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("#siteid", 3);
DataTable dt = new DataTable();
da.Fill(dt);
List<Poll> _poll1 = new List<Poll>();
foreach (DataRow row in dt.Rows)
{
Poll _poll = new Poll();
_poll.QuestionID = Convert.ToInt32(row["questionID"]);
_poll.Question = row["question"].ToString();
_poll.Published = Convert.ToInt32(row["visible"]);
_poll.Date = Convert.ToDateTime(row["Added_Date"]);
}
return _poll1.ToArray();
}
public class Poll
{
public Poll() { }
private int _questionId, _published;
private string _question;
private DateTime _date;
public int QuestionID
{
get { return _questionId; }
set { _questionId = value; }
}
public string Question
{
get { return _question; }
set { _question = value; }
}
public DateTime Date
{
get { return _date; }
set { _date = value; }
}
public int Published
{
get { return _published; }
set { _published = value; }
}
}
</code>
and below is my script.
<code>
$(this).load(function () {
$.ajax({
type: "POST",
url: "AddPollAJax.aspx/GetPollDetailed",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
for (i = 0; i < data.length; i++) {
alert(data[i].QuestionID);
}
},
error: function (data) {
alert("Error: " + data.responseText);
}
});
});
</code>
can any one please help me to resolve this issue, i am very curious about it.
Assuming your service is configured correctly to return JSON data, issue lies at your js code fragment for success callback i.e.
success: function (data) {
for (i = 0; i < data.length; i++) {
alert(data[i].QuestionID);
}
},
MS ASP.NET script services always return a wrapped JSON due to security issues, so you need unwrap resultant JS object to get the actual data. So you need to change the code to
success: function (result) {
var data = result.d; // actual response will be in this property
for (i = 0; i < data.length; i++) {
alert(data[i].QuestionID);
}
},
BTW, ASP.NET Web Services are now considered legacy, so I will suggest you to migrate to WCF services instead.

Invalid JSON issue with asp.net jquery ajax call

Invalid JSON issue with asp.net jquery ajax call
Hi,
I am facing a strange issue while receiving response for an ajax call when I fire it to page.
I am using jquery to post data to the code behind
Javascript -
var jsonData = { "a": JSON.stringify(obj1),"b": JSON.stringify(obj2)};
jsonData = JSON.stringify(jsonData);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: 'Page.aspx/AjaxCall',
async: true, cache: true, data: jsonData, dataType: "json",
success: function (msg) {
alert('hurray! Success. ' + msg.d);
},
error: function (xhr, textstatus, errorThrown) {
alert('there was an error' + errorThrown);
}
});
C#
[WebMethod]
public static string AjaxCall(string a, string b)
{
Dictionary<object, Dictionary<string, object>> x = new Dictionary<object, Dictionary<string, object>>();
Dictionary<string, object> z = new Dictionary<string, object>();
Dictionary<object, object> y = new Dictionary<object, object>();
int u = 0;
int v = 0;
// deserialise z class
try
{
if (!string.IsNullOrEmpty(b))
{
z = (Dictionary<string, object>)new JavaScriptSerializer().Deserialize(b, typeof(Dictionary<string, object>));
if (z.ContainsKey("IsProctored"))
y.Add("BlurCount", z["BlurCount"]);
if (z.ContainsKey("IsCrossClicked"))
{
y.Add("IsCrossClicked", z["IsCrossClicked"]);
}
}
}
catch (InvalidOperationException ioe)
{
throw new Exception();
}
// deserialise items class
try
{
x = (Dictionary<object, Dictionary<string, object>>)
new JavaScriptSerializer().Deserialize(a, typeof(Dictionary<object, Dictionary<string, object>>));
}
catch (InvalidOperationException ioe)
{
throw new Exception();
}
if (u == 0)
{
throw new Exception();
}
if (v == 0)
{
throw new Exception();
}
if (x.Count == 0)
{
//throw new Exception();
}
try
{
// saves the data in backend
SaveResponse(u, v, x, y);
}
catch (Exception ex)
{
throw new Exception();
}
return "response fired";
}
The error that I get is 'Invalid JSON' (in Firebug's response tab of ajax call). Although it's straightforward ajax call.
I am kinda confused why this maay be happening because this it works on majority of the cases and fails on some other.
Any idea would be a great help.
thanks!
You have lots of throw Exception statements in there.
Any of those would trigger the invalid Json response!
You need to trap error messages and instead of throwing an exception, return some valid Json to indicate the problem.
Also for debugging I suggest using Firefox with HttpFox installed - you can then analyze the exact content of any responses, whether by Ajax or not. I find it an invaluable and free tool for situations like this.
It was because of some strange issue with Firefox 3.6. I restarted the browser and tested it again and things were working as expected.

Resources