在 ASP.NET MVC3 中如何查看请求?

发布于 2024-11-01 05:11:07 字数 1122 浏览 2 评论 0原文

我使用简单的路线

routes.MapRoute(
    "Default2", // Route name
    "{cliurl}/{id}", // URL with parameters
    new { cliurl = "none", controller = "ABook", action = "Index", id = "none" } // Parameter defaults
);

routes.MapRoute(
    "Default", // Route name
    "{cliurl}/{controller}/{action}/{id}", // URL with parameters
    new { cliurl = "none", controller = "ABook", action = "Index", id = "none" } // Parameter defaults
);

,当我调试网站(VS2010 SP1)时,我的 ABook 控制器中有一个断点,在 Index 操作方法内只包含:

//
// GET: /ABook/
public ActionResult Index()
{
    if (currentClient == null)
        return RedirectToAction("Empty");

    return View();
}

//
// GET: /Empty/
public ActionResult Empty()
{
    return View();
}

问题是,当我将其插入浏览器中时:

http://localhost:14951/client_name/hashed_id

我在该断点中得到了 3 个中断。

我怎样才能看到世界上正在发生的事情?为什么我只请求了1次却又出现了3次,浏览器到底请求的是什么?

我只能获取路由参数,并且第一个参数正确,但第二个和第三个参数使用默认值,我尝试在 RequestContext 中导航,但看不到任何有用的东西:(

只是想知道是否有办法真正查看所请求的内容。

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", // Route 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

拧巴小姐 2024-11-08 05:11:07

如果控制器内部有断点,则可以使用 watch 来简单地创建新的 watch。输入 Request 并搜索...

If you have breakpoint inside controller you can use watch where you can simply create new watch. Type in Request and search it...

请爱~陌生人 2024-11-08 05:11:07

每个控制器中都存在一个名为Request的属性。它实际上是在 System.Web.Mvc.Controller 中定义的,它是所有控制器的超类。该属性返回实际的 Request 对象作为 HttpRequestBase 并公开诸如 InputStream、Headers、HttpMethod 等字段。

至于为什么您要点击索引方法 3 次,我确信浏览器发出的其他请求(例如图像、javascript 和其他现有文件)也会由您定义的路由处理。简而言之,您的路由定义过于通用并且处理意外请求。您可以使用 Route.IgnoreRoute("Path/to/Existing/Files") 或通过添加 RouteConstraints 使路线更加具体来纠正此问题。如果您想知道如何操作,请发表评论。

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.

╰ゝ天使的微笑 2024-11-08 05:11:07

您可以使用 fiddler 来查看浏览器请求的内容,或者您​​可以尝试 routdebugger 从 Nuget 下载。

You can use fiddler to see what the browser requests or you could try the routdebugger download from Nuget.

小情绪 2024-11-08 05:11:07

我知道其他人已经尝试过这个...他们是正确的:

使用 Request 对象来找出所请求的内容。这可能是您的控制器处理不当的原因。在该方法中从 Request 进行调试时铲除一些输出,例如原始 url。这可能会回答这个问题。

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.

归途 2024-11-08 05:11:07

作为建议,为什么不为应用程序连接 BeginRequest 事件处理程序,这将允许您查看通过的每个请求。还有可以检查的 HttpContext.Current.Request.Url 对象

    // 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);
    }

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文