vir*_*vir 30 asp.net-mvc asp.net-mvc-routing
我有一个空的ASP.NET应用程序,我添加了一个index.html文件.我想将index.html设置为网站的默认页面.
我试图右键单击index.html并设置为起始页面,当我运行它时,url是:http://localhost:5134/index.html
但我真正想要的是当我输入:时http://localhost:5134
,它应该加载index.html页面.
我的路线配置:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
vir*_*vir 31
我在路由配置中添加了一条指令来忽略空路由,这解决了我的问题.
routes.IgnoreRoute("");
Run Code Online (Sandbox Code Playgroud)
Ale*_*éau 21
当@vir回答时,默认情况下添加routes.IgnoreRoute("");
到RegisterRoutes(RouteCollection routes)
RouteConfig.cs中应该找到的内容.
这是方法的样子:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
原因是ASP.NET MVC接管URL管理,默认情况下路由是所有无扩展名URL都由web.config中定义的无扩展Url处理程序控制.
有一个详细的解释在这里.
假设Web应用程序在IIS中运行,则可以在web.config文件中指定默认页面:
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="index.html" />
</files>
</defaultDocument>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
小智 5
创建一个新的控制器DefaultController。在索引操作中,我写了一行重定向:
return Redirect("~/index.html")
Run Code Online (Sandbox Code Playgroud)
在RouteConfig.cs中,更改路由的controller =“ Default”。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
38933 次 |
最近记录: |