【问题标题】:How to open static html file in asp.net mvc without controller & view如何在没有控制器和视图的 asp.net mvc 中打开静态 html 文件
【发布时间】:2018-10-31 17:31:28
【问题描述】:

我有一些 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

【问题讨论】:

  • 据我所知,除非文件位于 Views 文件夹中,否则您不需要调整 web.config。例如,如果您有一个文件夹名称“StaticFiles”,那么一个文件“index.html”您应该能够通过yourapp/StaticFiles/index.html 访问它
  • 能否分享一下调用html文件的代码。
  • 默认情况下,甚至不需要 routes.**IgnoreRoute("{file}.html");**。默认情况下,Asp.Net MVC 能够托管静态 html。无需配置。您的问题必须位于其他地方。我建议您查看您的服务器日志并处理所有异常以确定您的问题到底是什么。查看stackify.com/aspnet-mvc-error-handling 了解如何处理 ASP.Net MVC 中的错误

标签: asp.net-mvc


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多