C#.net映射路由到html文件
如何将MapRoute从根目录设置为index.html文件 我想转到 我尝试:C#.net映射路由到html文件,c#,.net,asp.net-mvc-routing,maproute,asp.net-mvc-5,asp.net-core,C#,.net,Asp.net Mvc Routing,Maproute,Asp.net Mvc 5,Asp.net Core,如何将MapRoute从根目录设置为index.html文件 我想转到 我尝试: routes.MapRoute( name: "Root", url: "index.html" ); 在RegisterRoutes内部,但返回404(未找到) 谢谢这有几个问题 首先,Web.config中的默认配置将拒绝任何包含字符的URL。如果您确实为此使用了路由,您会 也就是说,你特别提到了“文件”。MVC不服务于文件,它服务于资源(内容或
routes.MapRoute(
name: "Root",
url: "index.html"
);
在RegisterRoutes内部,但返回404(未找到)
谢谢这有几个问题 首先,Web.config中的默认配置将拒绝任何包含
字符的URL。如果您确实为此使用了路由,您会
也就是说,你特别提到了“文件”。MVC不服务于文件,它服务于资源(内容或流)。大多数(如果不是全部的话)web服务器已经设置为提供文件服务(特别是html
、css
和js
文件),因此除非有特定的原因,否则根本没有理由让ASP.NET/MVC参与其中。根据您使用的web服务器的不同,您应该查阅有关如何将index.html
设置为默认页面的文档
如果您决定让MVC而不是web服务器来处理请求,此路由配置将不起作用,因为它不提供所需的路由值controller
和action
,以便将URL实际路由到MVC控制器
routes.MapRoute(
name: "Root",
url: "index.html",
defaults: new { controller = "Home", action = "Index" }
);
我还应该指出,这个答案适用于没有OWIN的MVC5。为便于将来参考,如果您指定或指示您所指的版本,将非常有用
这有几个问题 首先,Web.config中的默认配置将拒绝任何包含
字符的URL。如果您确实为此使用了路由,您会
也就是说,你特别提到了“文件”。MVC不服务于文件,它服务于资源(内容或流)。大多数(如果不是全部的话)web服务器已经设置为提供文件服务(特别是html
、css
和js
文件),因此除非有特定的原因,否则根本没有理由让ASP.NET/MVC参与其中。根据您使用的web服务器的不同,您应该查阅有关如何将index.html
设置为默认页面的文档
如果您决定让MVC而不是web服务器来处理请求,此路由配置将不起作用,因为它不提供所需的路由值controller
和action
,以便将URL实际路由到MVC控制器
routes.MapRoute(
name: "Root",
url: "index.html",
defaults: new { controller = "Home", action = "Index" }
);
我还应该指出,这个答案适用于没有OWIN的MVC5。为便于将来参考,如果您指定或指示您所指的版本,将非常有用
如果您使用的是ASP.NET Core,则可以启用静态文件服务: 在Startup.cs中添加
UseStaticFiles()
public void Configure(IApplicationBuilder app)
{
...
app.UseMvcWithDefaultRoute();
app.UseStaticFiles();
}
您的index.html需要位于wwwroot文件夹中如果您使用的是ASP.NET Core,则可以启用静态文件服务: 在Startup.cs中添加
UseStaticFiles()
public void Configure(IApplicationBuilder app)
{
...
app.UseMvcWithDefaultRoute();
app.UseStaticFiles();
}
您的index.html需要位于wwwroot文件夹中ASP.NET路由映射器可以路由到文件或控制器 尝试以下示例:(您需要将.html文件重命名为.aspx,但不必对其进行任何其他更改。) 在路由配置文件中:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// route default URL to index.aspx
routes.MapPageRoute(
routeName: "DefaultToHTML",
routeUrl: "",
physicalFile: "~/index.aspx",
checkPhysicalUrlAccess: false,
defaults: new RouteValueDictionary(),
constraints: new RouteValueDictionary { { "placeholder", ""} }
);
// other routes go here...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
ASP.NET路由映射器可以路由到文件或控制器 尝试以下示例:(您需要将.html文件重命名为.aspx,但不必对其进行任何其他更改。) 在路由配置文件中:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// route default URL to index.aspx
routes.MapPageRoute(
routeName: "DefaultToHTML",
routeUrl: "",
physicalFile: "~/index.aspx",
checkPhysicalUrlAccess: false,
defaults: new RouteValueDictionary(),
constraints: new RouteValueDictionary { { "placeholder", ""} }
);
// other routes go here...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
对我有用的是我发现了什么。只需将此添加到
注册表项
(当然,在routes.MapRoute
之前):
对我有用的是我发现了什么。只需将此添加到
注册表项
(当然,在routes.MapRoute
之前):
我懂了。。。我正在用MVC建立一个小网站。我应该使用webforms吗?顺便说一句,我注意到通过AJAX调用加载控制器方法花费的时间太长(大约3秒),使用哪种技术是一个主观问题,当然这取决于您的需求。这是你需要自己研究和评估的东西。至于为什么您的AJAX调用花费的时间太长,我建议您在StackOverflow上发布另一个问题-这很可能与您正在使用的技术无关,而与您正在使用的特定代码或配置有关。我明白了。。。我正在用MVC建立一个小网站。我应该使用webforms吗?顺便说一句,我注意到通过AJAX调用加载控制器方法花费的时间太长(大约3秒),使用哪种技术是一个主观问题,当然这取决于您的需求。这是你需要自己研究和评估的东西。至于为什么AJAX调用花费的时间太长,我建议您在StackOverflow上发布另一个问题——这很可能与您使用的技术无关,而与您使用的特定代码或配置有关。