简体   繁体   English

在ASP.NET MVC中托管后,静态Html页面无法正常工作

[英]Static Html page is not working after hosting in ASP.NET MVC

In my MVC project, There is a Default.html file in root folder and route this file as a default route. 在我的MVC项目中,根文件夹中有一个Default.html文件,并将此文件作为默认路由路由。

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

It's working fine when I access it like http://localhost:51353/Default.html 当我访问http://localhost:51353/Default.html时它工作正常

I host (include this static file) this project in my web server. 我在我的Web服务器中托管(包含此静态文件)此项目。 But it's showing error: 但它显示错误:

404错误

Is there any additional configuration needed to do this ? 是否需要执行其他配置?

If you want to host a static HTML page within an ASP.net MVC project then you need to configure your routing configuration in MVC to ignore requests for those pages. 如果要在ASP.net MVC项目中托管static HTML page ,则需要在MVC配置路由配置以忽略对这些页面的请求。

It worked locally because you may have set it as start page in Visual Studio. 它在本地工作,因为您可能已将其设置为Visual Studio中的起始页。 For this to work you need to tell MVC to ignore the route if its for the HTML page or ASPX page . 为此,您需要告诉MVC忽略HTML pageASPX page的路由。 Find your routing configuration section, it's in RouteConfig.cs under the App_Start folder. 寻找你的路由配置部分,它在RouteConfig.csApp_Start文件夹。 Use the IgnoreRoute() method to tell Routing to ignore the specific paths. 使用IgnoreRoute()方法告诉Routing忽略特定路径。

routes.IgnoreRoute("Default.html"); //ignore the specific HTML page

Now MVC ignores a request to load the Default.html page and leaves IIS to handle the resource. 现在, MVC忽略了加载Default.html页面的请求,并让IIS处理资源。

根据MVC路由,您无法将静态文件映射到路由表,因为当MVC路由机制提供直接访问物理存在的静态文件时。

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

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