简体   繁体   中英

ASP.NET MVC 2 One Route Works, One Route Doesn't

I have two routes in my ASP.NET MVC application.

The first is working fine - it's an ActionResult that returns a view.

The second is on the same controller and is an ActionResult that returns a Json response. It takes a couple of additional paramaters.

This second route is working on my dev machine, but when I deploy it to the server I get back a blank response. Any suggestions will be gratefully received.

I have also copy-pasted the route into a browser to eliminate any issues in the jQuery JavaScript.

The method

[HttpGet]
public ActionResult CheckSku(string id, string brand) {
 CheckSkuModel model = new CheckSkuModel();

 model.Id = id;
 model.Brand = brand;

 return Json(model, JsonRequestBehavior.AllowGet);
}

The routes

routes.MapRoute(
  "Default", // Route name
  "{controller}.mvc/{action}/{id}", // URL with parameters
  new {
    controller = "Orders", action = "Send", id = ""
  } // Parameter defaults
);

routes.MapRoute(
  "CheckSku", // Route name
  "{controller}.mvc/{action}/{id}/{brand}", // URL with parameters
  new {
    controller = "Orders", action = "CheckSku", id = "", brand = ""
  } // Parameter defaults
);

Two thing you can quickly check that may help: 1. Swap the two routes around so "CheckSku" is above "default" 2. Make the "CheckSku" more specific so will look something like:

routes.MapRoute(
  "CheckSku", // Route name
  "Orders.mvc/CheckSku/{id}/{brand}", // URL with parameters
  new {
    controller = "Orders", action = "CheckSku", id = "", brand = ""
  } // Parameter defaults
);

that was another controller doesn't pick up the url by mistake.

An alternative is the use the routelink helper when generating the url so it points to the correct route.

When you say you "get back a blank response", do you mean you're not getting an error, but that the server isn't returning anything? What makes you think this is a routing issue?

Some troubleshooting tips:

  1. Use Fiddler to examine the HTTP request you're making and the raw HTTP response the server is sending back. If you're getting a 500 response code then check the response for error information.

  2. Can you attach a remote debugger to the server? This might give you a chance to investigate any exceptions that are raised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM