什么是正确的配置或路由,以使Default.aspx,“"和“〜/"成为可能?不被路由? [英] What's the proper configuration or routing so that Default.aspx, "", and "~/" don't get routed?

查看:154
本文介绍了什么是正确的配置或路由,以使Default.aspx,“"和“〜/"成为可能?不被路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我瞥见了.

它在路线"标签上突出显示了一个空行

it's highlighting an empty line on the routes tab

Match Area Url Data constraints DataTokens

True Root -- --

在本地,cassini似乎无法正确模拟虚拟目录,因此从localhost更改为localhost/site似乎并没有赋予任何其他测试见识.

Locally it seems cassini doesn't properly emulate a virtual directory so changing from localhost to localhost/site doesn't seem to grant any additional testing insight.

  • 本地测试
    • 卡西尼(Windows 7(有32位和64位可用))
    • 似乎没有使用或不允许集成
    • Local testing
      • cassini (windows 7 (32 and 64 bit are available))
      • doesn't seem to use or allow integrated
      • 集成了IIS7
      • 尝试使用http路由设置将/推送到/site,但是我遇到了斜杠问题(/site会路由到/site/site,而/site/会按预期工作
      • IIS7 Integrated
      • Tried a http routing setting to push / to /site however I ran into trailing slash issues (/site would route to /site/site, while /site/ would work as expected
      • 尝试了几种设置runAllManagedModules=false的错误的方法
      • 使用<remove name="UrlRoutingModule-4.0" /> <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />" >链接
      • tried a few ways of setting runAllManagedModules=false with various errors
      • using <remove name="UrlRoutingModule-4.0" /> <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" /> that was linked

      我需要/site(/~)个请求才能转到 ~/Default.aspx 也会想办法让/也去那里.

      I need /site (/~) requests to go to ~/Default.aspx would love for a way to have / go there as well.

      我该怎么做?

      推荐答案

      要将Default.aspx启用为默认网址,您只需删除路由处理,就不会出现所有路由.

      To enable Default.aspx as the default url you just need to remove your route handling so that there's no catch-all route.

      默认情况下,所有MVC 3项目都具有:

      By default all MVC 3 projects have:

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

      这是一条通行的路由,将与您在您的网址中传递的所有匹配.如果删除此选项,但未定义路径,则它将使用您在web.config中设置为默认页面的任何内容.

      This is a catch-all route that will match everything you pass in your url. If you remove this and no path is defined then it will use whatever you set up as your default page in your web.config.

      要将/site映射到Default.aspx,您只需添加一条特殊路线:

      To map /site to Default.aspx you just need to add a special route:

      routes.MapPageRoute("SitetoDefault", "site", "~/Default.aspx");
      

      这将显示/site并将其路由到您的默认页面.

      This will see the /site and route it to your default page.

      在将任何其他路由添加到路由表时,您必须要小心.它们都必须定义一个控制器,或者您必须添加路由约束.像这样:

      You'll have to be careful when adding any other routes to your routing table. All of them will have to have a controller defined or you'll have to add a routing constraint. Something like this:

      //controller defined
      routes.MapRoute(
          "Default", // Route name
          "{controller}/{action}/{id}", // URL with parameters
          new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
      );
      
      //route constraint
      routes.MapRoute(
          "Default", // Route name
          "{controller}/{action}/{id}", // URL with parameters
          new {
              controller = "Home",
              action = "Index",
              id = UrlParameter.Optional
          }, // Parameter defaults
          new { controller = "^(products)|(account)|(home)$" }
      );
      

      有关创建更复杂的路由的详细信息,请参见: http://www.asp.net/mvc/tutorials/creating-a-custom-route-constraint-cs

      Here is more detail on creating more complex routing: http://www.asp.net/mvc/tutorials/creating-a-custom-route-constraint-cs

      这篇关于什么是正确的配置或路由,以使Default.aspx,“"和“〜/"成为可能?不被路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆