简体   繁体   English

为什么在ASP.NET MVC示例中路径信息路由不起作用?

[英]Why Is the Path Info Routing Not Working in ASP.NET MVC Example?

I am reading the book C# 6.0 and the .NET 4.6 Framework and I was looking at the customized routing example routes.MapRoute("Contact", "Contact/{*pathinfo}", new { controller = "Home", action = "Contact" }); 我正在阅读C#6.0和.NET 4.6框架 ,正在查看自定义的路由示例routes.MapRoute("Contact", "Contact/{*pathinfo}", new { controller = "Home", action = "Contact" }); . The authors says that this should allow me to put in the url http://localhost:64173/Home/Contact/Foo/Bar and still be directed to the Contact page, because of the addition of *pathinfo . 作者说,这应该允许我输入url http://localhost:64173/Home/Contact/Foo/Bar并且由于添加了*pathinfo ,因此仍可以定向到“联系”页面。 However, it is simply giving me a 404 Not Found. 但是,它只是给我一个404 Not Found。 Specifically the authors state: 作者特别指出:

...adding {*pathinfo} to the pattern... allows any number of additional URL parameters... Now when you enter the URL http://localhost:64173/Home/Contact/Foo/Bar it still shows the Contact page. ...在模式中添加{* pathinfo} ...允许任意数量的其他URL参数...现在,当您输入URL http://localhost:64173/Home/Contact/Foo/Bar它仍显示联系人页。

But I still get the 404 Error. 但是我仍然收到404错误。 I noted that http://localhost:64173/Home/Contact/Foo as well as http://localhost:64173/Contact/Foo/parm1/parm2/parm3 . 我注意到http://localhost:64173/Home/Contact/Foo以及http://localhost:64173/Contact/Foo/parm1/parm2/parm3 All of these redirect back to the Contact page. 所有这些都重定向回“联系人”页面。 Here is what the full routing looks like: 完整的路由如下所示:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute("Contact", "Contact/{*pathinfo}", new { controller = "Home", action = "Contact" });
            routes.MapRoute("About", "About/{*pathinfo}", new { controller = "Home", action = "About" });
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

Thanks, I know this probably obvious, but I can't tell if the author made a mistake or if I'm missing something in my routes. 谢谢,我知道这可能很明显,但是我无法确定作者是否犯了错误,或者我是否在路线中遗漏了一些东西。

Note1: I also tried *pathInfo (with the "I" capitalized). 注意1:我也尝试了* pathInfo(将“ I”大写)。 This did not work either. 这也不起作用。

Note2: Navigating to http://localhost:64173/Home/Contact/Foo works just fine. 注意2:浏览至http://localhost:64173/Home/Contact/Foo可以正常工作。

You can't handle a request to http://localhost/Contact/Foo from a controller other than ContactController unless you inherit ContactController from something other than the base controller and do some implementation work to make that kind of Url rewrite happen. 您不能处理除ContactController之外的其他控制器对http://localhost/Contact/Foo的请求,除非您从基本控制器以外的其他类继承了ContactController并进行一些实现工作以使这种Url重写发生。 The excerpt to which you are referring simply allows you to pass parameters as Url segments without explicitly specifying all the parameters in the Url template. 您所引用的摘录仅允许您将参数作为Url段传递,而无需在Url模板中明确指定所有参数。

I have encountered with this problem while reading the book 我在看书时遇到了这个问题

Pro C# 7: With .NET and .NET Core 8th Edition by Andrew Troelsen and Phil Japiks. Pro C#7:使用Andrew Troelsen和Phil Japiks编写的.NET和.NET Core第8版。

There is wrote "Add a new route" : 上面写着"Add a new route"

routes.MapRoute("Contact", "Contact/{*pathinfo}", new { controller = "Home", action = "Contact" });

and after you add a new route you need to open test URL: 添加新路由后,您需要打开测试URL:

http://localhost:60466/Home/Contact/Foo/Bar

but there is a mistake, you need to open URL above, without 但是有一个错误,您需要打开上面的网址,而无需

Home

like this http://localhost:60466/Contact/Foo/Bar 像这样http://localhost:60466/Contact/Foo/Bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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