为什么 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 });

在控制器中,我将视图设置为:-

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 }
);

The default controller should usually be the last route registered

假设控制器

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