简体   繁体   中英

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

My application in ASP.NET MVC4 I have:

    <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:

  <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

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

When you're getting www.website.com/mycode , Routing thinks that "mycode" is a controller name. 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. You will have to change the url template.

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. Also the default values for controller and action will kick in and you will get the result you are after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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