首页 > 解决方案 > 如何在没有控制器和视图的 asp.net mvc 中打开静态 html 文件

问题描述

我有一些 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 已经创建,并且关于我称之为视图的控制器索引操作。所以它正在工作,但我直接从文件夹打开文件然后它没有打开。

我正面临错误

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

标签: asp.net-mvc

解决方案


我有我创建的动态页面路线如下

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

推荐阅读