繁体   English   中英

为什么ASP.NET MVC URL需要索引

[英]Why do ASP.NET MVC URLs need Index

努力解决以下问题

http://localhost/star/regulus

唯一可行的方法是拥有一个网址

http://localhost/star/index/regulus

这是我的路线

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

routes.MapRoute( "", "star/{sstar}", new { Controller = "star", id = UrlParameter.Optional });

然后在Controller中,将View设置为:-

return View("~/views/star.cshtml", oStar);

这样您就可以看到我没有使用Index.cshtml

关于如何获得最佳网址的任何想法,而不是第二个。

您需要重新安排路线

routes.MapRoute(
    name: "StarRoute",
    url: "star/{sstar}",
    defaults: new { controller = "Star", action = "Star", sstar = UrlParameter.Optional}
);

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

默认控制器通常应该是最后注册的路由

假设控制器

public class StarController :Controller {
    public ActionResult Star(string sstar){
        //...other code that uses sstar parameter
        return View();
    }
}

视图应位于...

~/Views/Star/Star.cshtml 

...以使以下URI起作用:

http://localhost/star/regulus  
http://localhost/star?sstar=regulus
http://localhost/star/sirius  
http://localhost/star?sstar=sirius

暂无
暂无

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

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