如何在没有控制器和视图的情况下在ASP.NET MVC中打开静态HTML文件

时间:2018-10-27 09:33:56

标签: asp.net-mvc

我有一些HTML文件存储在一个文件夹中,我想打开这些HTML文件。

我在route.config文件中添加了以下代码:

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

现在,此about.html URL已创建,并且关于控制器索引操作已被称为view。因此它正在工作,但我直接从文件夹中打开文件,但未打开。

我正面临错误

  

HTTP错误500.0-内部服务器错误
  内部服务器错误

最可能的原因是:

IIS received the request; however, an internal error occurred during the processing of the request. The root cause of this error depends on which module handles the request and what was happening in the worker process when this error occurred.
IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly.
IIS was not able to process configuration for the Web site or application.
The authenticated user does not have permission to use this DLL.
The request is mapped to a managed handler but the .NET Extensibility Feature is not installed.

我希望路径为example.com/Folder/test.html

1 个答案:

答案 0 :(得分:0)

我为创建的路线创建了动态页面,如下所示

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

当我为.html扩展名添加上述路由时,上述路由不起作用,因此我更改了web.config中的配置。之后,它开始工作。但是当我调用静态html文件时,它显示了错误。

为此,我在控制器中的代码下面创建了一个小工具,最后我得到了解决方案

public ActionResult Index(string page)
    {
        var pageRoute = "~/uploads/" + page + ".html";
        var staticPageToRender = new FilePathResult(pageRoute, "text/html");
        return staticPageToRender;
    }