简体   繁体   English

没有控制器和动作X的Asp.Net MVC路由X Web.config

[英]Asp.Net MVC Routing without controller and action X Web.config

My application in ASP.NET MVC4 I have: 我在ASP.NET MVC4中的应用程序有:

    <authentication mode="Forms">
      <forms loginUrl="~/Logon/Autentica" timeout="120"></forms>
    </authentication>

routes: 路线:

routes.MapRoute(
                name: "Abcdef",
                url: "{controller}/{action}/{conte}",
                defaults: new { controller = "CampanhaResposta", action = "Resposta" }
            );

Web.config: Web.config文件:

  <location path="CampanhaResposta/Resposta">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>

It don't works: www.website.com/mycode but it's working: www.website.com/CampanhaResposta/Resposta/mycode 它不起作用: www.website.com/mycode但是它起作用: www.website.com/CampanhaResposta/Resposta/mycode

Why? 为什么?

You need change your routing to: 您需要将路由更改为:

routes.MapRoute(
                name: "Abcdef",
                url: "{action}/{conte}",
                defaults: new { controller = "CampanhaResposta", action = "Resposta", conte = UrlParameter.Optional}
            );

It will return: www.website.com/Resposta 它将返回: www.website.com/Resposta

When you're getting www.website.com/mycode , Routing thinks that "mycode" is a controller name. 当您访问www.website.com/mycode ,Routing认为“ mycode”是控制器名称。 Even though you've provided default values for "controller" and "action" route parameters, they are not used because url: "{controller}/{action}/{conte}" expects "conte" the last. 即使您已提供“控制器”和“动作”路由参数的默认值,也不会使用它们,因为url: "{controller}/{action}/{conte}"期望最后一个“ conte”。 You will have to change the url template. 您将必须更改url模板。

For example, if you change it to 例如,如果将其更改为

url: "{conte}/{controller}/{action}"

and then get www.website.com/mycode again, "mycode" will correctly be treated as a value of the "conte" route variable. 然后再次访问www.website.com/mycode ”将被正确地视为“ conte”路由变量的值。 Also the default values for controller and action will kick in and you will get the result you are after. 控制器和操作的默认值也会生效,您将获得想要的结果。

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

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