in ASP.NET MVC3 how can I see the request?
I'm using a simple route as
routes.MapRoute(
"Default2", // Route name
"{cliurl}/{id}", // URL with parameters
new { cliurl = "none", controller = "ABook", action = "Index", id = "none" } // Parameter defaults
);
routes.MapRoute(
"Default", // R开发者_Python百科oute name
"{cliurl}/{controller}/{action}/{id}", // URL with parameters
new { cliurl = "none", controller = "ABook", action = "Index", id = "none" } // Parameter defaults
);
and when I debug the website (VS2010 SP1), I have a breakpoint in my ABook
Controller, inside the Index
action method witch contains only:
//
// GET: /ABook/
public ActionResult Index()
{
if (currentClient == null)
return RedirectToAction("Empty");
return View();
}
//
// GET: /Empty/
public ActionResult Empty()
{
return View();
}
The thing is that, when I insert this in the browser:
http://localhost:14951/client_name/hashed_id
I get 3 breaks in that breakpoint.
How can I see what in the world is going on? why 3 times when I just requested 1, what is exactly the browser requesting?
I can only get the Route Parameters and I do get the first correct, but 2nd and 3rd are using the default values, and I tried to navigate through the RequestContext
and I can't see anything useful :(
Just want to know if there is a way to really see what's been requested.
I ended up using Glimpse
http://getglimpse.com/
http://www.balexandre.com/temp/2011-05-28_1854.png
If you have breakpoint inside controller you can use watch where you can simply create new watch. Type in Request
and search it...
In every Controller there exists a property called Request. It is actually defined in System.Web.Mvc.Controller which is the superclass of all controllers. The property returns the acutal Request object as HttpRequestBase and exposes fields like InputStream, Headers, HttpMethod so on and so forth.
As for why you are hitting the index method 3 times, I'm sure that other requests made by the browser, say for example for images and javascript and other existing files, also are handled by your route defined. In short your route defenition is too generic and handles unexpected requests. You can correct this by using Route.IgnoreRoute("Path/to/Existing/Files") or by making your route more specific by adding RouteConstraints. Leave a comment if you want to know how to do that.
You can use fiddler to see what the browser requests or you could try the routdebugger
download from Nuget.
I know others have sort-of made a stab at this... they are correct:
Use the Request
object to find out what is being requested. It's probably something incorrectly being handled by your controller. Shovel some output while debugging from Request
in that method, such as the raw url. That will likely answer the question.
As a suggestion, why not hook up the BeginRequest
event handler for the application which will allow you to see every request coming through. There is also the HttpContext.Current.Request.Url
object which can be inspected
// Global.asax
public MvcApplication()
{
BeginRequest += new EventHandler(MvcApplication_BeginRequest);
}
void MvcApplication_BeginRequest(object sender, EventArgs e)
{
Debug.WriteLine("[Start] Requested Url: " + HttpContext.Current.Request.RawUrl);
}
精彩评论