ASP.NET MVC 路由问题

发布于 2024-07-27 21:49:20 字数 523 浏览 5 评论 0原文

我刚刚开始使用 ASP.NET MVC,有两个路由问题。

  1. 如何设置以下路线 在 ASP.NET MVC 中?

    domain.com/about-us/ 
      域名.com/contact-us/ 
      域名.com/staff-bios/

    我不想在实际网址中指定控制器,以保持网址较短。 如果网址看起来像这样:

    domain.com/company/about-us/ 
      域名.com/company/contact-us/ 
      域名.com/company/staff-bios/ 
       
    
      

    这对我来说更有意义,因为我可以添加一个 CompanyController 并为“关于我们”、“联系我们”、“员工简介”设置 ActionResults 并返回适当的视图。 我缺少什么?

  2. Global.asax 中默认路由规则中的“默认”名称有何用途? 它有什么用途吗?

谢谢你!

I just started using ASP.NET MVC and I have two routing questions.

  1. How do I set up the following routes
    in ASP.NET MVC?

    domain.com/about-us/
    domain.com/contact-us/
    domain.com/staff-bios/

    I don't want to have a controller specified in the actual url in order to keep the urls shorter. If the urls looked liked this:

    domain.com/company/about-us/
    domain.com/company/contact-us/
    domain.com/company/staff-bios/
    

    it would make more sense to me as I can add a CompanyController and have ActionResults setup for about-us, contact-us, staff-bios and return appropriate views. What am I missing?

  2. What purpose does the name "Default" name have in the default routing rule in Global.asax? Is it used for anything?

Thank you!

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

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

发布评论

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

评论(3

紫﹏色ふ单纯 2024-08-03 21:49:20

我首先回答你的第二个问题 - “默认”只是路线的名称。 如果您需要按名称引用路由,例如当您想要从路由生成 URL 时,可以使用此方法。

现在,对于您想要设置的 URL,您可以绕过控制器参数,只要您同意始终指定相同的控制器作为默认值即可。 该路由可能看起来像这样:

{action}/{page}

确保它是在其他路由之后声明的,因为这将匹配许多您不打算匹配的 URL,因此您希望其他路由首先对其进行破解。 像这样设置:

routes.MapRoute(null, "{action}/{page}", 
                 new { controller = "CompanyController", action = "Company", page = "contact-us" } );

当然,MyDefault 控制器中的操作方法“Company”需要有一个“string page”参数,但这应该可以满足您的要求。 您的 Company 方法将简单地检查视图是否存在(无论页面参数是什么),如果不存在则返回 404,如果存在则返回视图。

I'll answer your second question first - the "Default" is just a name for the route. This can be used if you ever need to refer to a route by name, such as when you want to do URL generation from a route.

Now, for the URLs that you want to set up, you can bypass the controller parameter as long as you're ok with always specifying the same controller as a default. The route might simply look like this:

{action}/{page}

Make sure that it's declared after your other routes, because this will match a lot of URLs that you don't intend to, so you want the other routes to have a crack at it first. Set it up like so:

routes.MapRoute(null, "{action}/{page}", 
                 new { controller = "CompanyController", action = "Company", page = "contact-us" } );

Of course your action method "Company" in your MyDefault controller would need to have a "string page" parameter, but this should do the trick for you. Your Company method would simply check to see if the View existed for whatever the page parameter was, return a 404 if it didn't, or return the View if it did.

﹂绝世的画 2024-08-03 21:49:20

说到设置路线和 Phil Haack,我找到了他的 路线调试器是无价的。 当您不明白为什么使用特定路由来代替其他路由或学习如何设置特殊路由场景(例如您提到的路由场景)时,它是一个很好的工具。 它比任何其他资源都更能帮助我解决创建路线的许多复杂问题。

Speaking of setting up routes and Phil Haack, I have found his Route Debugger to be invaluable. It's a great tool for when you don't understand why particular routes are being used in place of others or learning how to set up special routing scenarios (such as the one you've mentioned). It's helped clear up many of the intricacies of route creation for me more than any other resource.

只为一人 2024-08-03 21:49:20

要回答有关 Global.asax 的第二个问题,它是一个可选文件,用于响应 ASP.NET 或 HTTP 模块引发的应用程序级别和会话级别事件。 Global.asax 文件位于 ASP.NET 应用程序的根目录中。如果您未定义,则假定您尚未定义任何应用程序处理程序或会话处理程序。 MVC框架使用路由引擎,在引擎中定义路由规则,以便将传入的URL映射到正确的控制器。
从控制器中,您可以访问 ActionName。 如果没有特定的控制器,将直接进入默认页面。 默认控制器是“Home”,其默认操作是“Index”。 请参阅 MSDN:

http://msdn。 microsoft.com/en-us/library/2027ewzw%28v=vs.100%29.aspx

请参阅 stackoverflow 问题

global.asax 的用途是什么?

这是默认路由的示例

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = 
  UrlParameter.Optional }
        );

To answer your second question about Global.asax, it an optional file used for responding to application level and session-level events raised by ASP.NET or HTTP modules. The Global.asax file resides in the root directory of the ASP.NET application.If you do not define it assumes you have not defined any application handler or session handler. MVC framework uses the routing engine to which the routing rules are defined in the engine, so as to map incoming URL to the correct controller.
From the controller, you can access the ActionName. If there is no specific controller, it will direct to the default page. The default controller is "Home" with its default action "Index". Refer to MSDN:

http://msdn.microsoft.com/en-us/library/2027ewzw%28v=vs.100%29.aspx

Refer to stackoverflow question

What is global.asax used for?

This is a sample of how a default route should look like

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = 
  UrlParameter.Optional }
        );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文