【问题标题】:Need help in opening .htm file in MVC在 MVC 中打开 .htm 文件时需要帮助
【发布时间】:2015-06-29 07:19:19
【问题描述】:

我正在使用 MVC4 和 Jquery。

我在通过 MVC 中的操作方法打开 .htm 文件时遇到问题。

这是我的代码:

<img src="~/Images/question_frame.png" style="margin-top:3px;height:18px;width:20px;" onclick="window.open('@Url.Action("Help", "Home", new { id = "NMCHelp"})', 'NMCHelp',toolbar=no, scrollbars=yes, resizable=yes, top=50, left=50,width=750, height=600');"/>

我的行动方法:

[HttpGet]
[Authorize]
public ActionResult Help(){
    var result = new FilePathResult("~/help/nmc/enu/Default.htm", "text/html");![enter image description here][1]
    return result;
}

我在尝试打开时遇到问题。我收到类似的错误 '$'未定义。

请告诉我如何通过操作方法打开 .htm 文件

【问题讨论】:

    标签: javascript jquery asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    不要尝试将其作为FilePathResult, 返回,这里没有必要使事情过于复杂。首先,将您的 HTM 文件放在项目中的文件夹中,例如 Files

    现在,将以下内容添加到您的 web.config 以防止未经授权访问您的文件:

    <location path="Files">
        <system.web>
            <authorization>
                <deny users="?" />
            </authorization>
        </system.web>
    </location>
    

    然后您可以通过以下方式链接到它:

    <img src="~/Images/question_frame.png" 
        style="margin-top:3px;height:18px;width:20px;" 
        onclick="window.open('/Files/Default.htm', 'NMCHelp',toolbar=no, scrollbars=yes, resizable=yes, top=50, left=50,width=750, height=600');"/>
    

    当您使用它时,您可能想要考虑删除那些内联样式并使用类名和内联 JavaScript。

    【讨论】:

    • 我试过了,但它不起作用或无法验证。即使我尝试使用 url 打开 htm 文件,它的打开也不要求验证
    【解决方案2】:

    为什么要为此创建控制器条目?这是一个普通的 .htm 文件,您可以直接浏览到 .htm 文件:

    <img src="~/Images/question_frame.png" style="margin-top:3px;height:18px;width:20px;" onclick="window.open('/help/nmc/enu/Default.htm', 'toolbar=no, scrollbars=yes, resizable=yes, top=50, left=50, width=750, height=600');" />
    

    或者,您的选择是将文件转换为 .cshtml 文件,在这种情况下,您可以使用控制器来仅 return View(),确保为 .cshtml 文件提供正确的名称并将其放入正确的文件夹中。

    【讨论】:

    • 我想为.htm文件申请授权,这样用户就不能在没有认证的情况下直接从外部打开。我将它添加到操作方法中。
    • 然后您需要将其创建为 .cshtml 并使用正确的 MVC 方式返回视图。
    • 在 MVC 视图中呈现 .htm 文件的任何帮助。
    • 你不能,当有人知道 .htm 文件的位置时,他们可以在那里浏览。这是由于 .htm 没有通过控制器呈现。或者,您可以考虑通过 IIS 执行 .htaccess 或某种形式的文件夹身份验证之类的操作。但是,我强烈建议按照预期的方式使用 MVC 控制器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2013-04-18
    • 2017-09-10
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    相关资源
    最近更新 更多