在 MVC 3.0 中使用规则将 http 重新路由到 https 不起作用
本文关键字:新路由 路由 不起作用 https http MVC 规则 | 更新日期: 2023-09-27 18:37:17
我必须将我网站上的所有请求从http路由到https,通过在Global.Asax.Cs中添加另一个类,我成功地做到
了这一点
// working part for redirecting http into https
protected void Application_BeginRequest()
{
if (!Context.Request.IsSecureConnection)
Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}
但在此之前,我尝试将规则插入 WebConfig
<rewrite>
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
</rule>
</rules>
</rewrite>
这是行不通的,但是我在这里看到很多帖子说规则是路由请求的最佳方式,我还在我的服务器上安装了URL重写。请任何人建议为什么它不起作用(我收到 500 内部服务器错误)。
当存在大量请求时,Global.asax 中的重新路由是否会影响性能。
请指教
我使用这个规则,它就像一个魅力:
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
没有任何关于错误的数据信息,我只能猜测您的 web.config 格式不正确,或者您将规则部分放在错误的位置或 URL 重写模块未正确安装