简体   繁体   English

如何在ASP.NET MVC3中通过HTTPS服务静态文件(在〜/ Content中)

[英]How to Serve Static Files (in ~/Content) Via HTTPS in ASP.NET MVC3

I have an ASP.NET MVC3 application to which I've just added an area. 我有一个ASP.NET MVC3应用程序,我刚刚在其中添加了一个区域。 Links inside the area (to images, JavaScript files, CSS etc.) default to https. 该区域内的链接(指向图像,JavaScript文件,CSS等)默认为https。 However, MVC doesn't seem to serve static content inside the ~/Content directory over https. 但是,MVC似乎并没有通过https在~/Content目录内提供静态内容。

I've pored over internet documentation, and I've found no information about how MVC treats the ~/Content directory differently, or how to get it to serve static files over https. 我已经仔细研究了Internet文档,但没有找到有关MVC如何以不同方式对待~/Content目录或如何使其通过https服务静态文件的信息。

I was hoping it would be as easy as a Web.config entry that says "allow static files to be served via https" but I've found no such thing. 我希望它像Web.config条目一样简单,该条目显示“允许通过https提供静态文件”,但我发现没有这种东西。

The only option I've come up with is to write a custom controller to serve my static content, but this seems so very wrong. 我想到的唯一选择是编写一个自定义控制器来提供我的静态内容,但这似乎非常错误。 What am I missing? 我想念什么?

UPDATE: to answer Joe's question below, the views themselves are being served over https. 更新:要回答乔下面的问题,视图本身是通过https提供的。 For static content, like JavaScript files, I'm using absolute URLs without protocol or domains. 对于静态内容(例如JavaScript文件),我使用的是没有协议或域的绝对URL。 For example: 例如:

<script "/Areas/Admin/Content/js/jquery.js"></script>

Because the protocol of the page is https, the browser is making a request for https://localhost:5300/Areas/Admin/Content/js/jquery.js (naturally). 因为页面的协议是https,所以浏览器正在自然地请求https://localhost:5300/Areas/Admin/Content/js/jquery.js Make the protocol http and that file is served correctly: make it https, and I get an error. 将协议设置为http并正确提供该文件:将其设置为https,然后收到错误消息。

UPDATE: I was foolishly not reading the error carefully. 更新:我愚蠢地没有仔细阅读错误。 There's more detail in there, but it's still got me baffled. 那里还有更多细节,但这仍然让我感到困惑。 The error I get when I try to access https://localhost:5300/Areas/Admin/Content/js/jquery.js is: 当我尝试访问https://localhost:5300/Areas/Admin/Content/js/jquery.js时遇到的错误是:

Parser Error Message: An error occurred loading a configuration file: 
Failed to start monitoring changes to
'C:\Users\ethan\Documents\gitwork\SampleWebSite\admin\content\web.config'
because access is denied.

Why is it looking for a Web.config file in that directory? 为什么要在该目录中寻找Web.config文件?

一个简单的解决方案是您不使用“〜”

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

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