c# - 如何更改 Request.RawUrl 属性?

标签 c# asp.net-mvc

MVC 的 BeginForm 帮助程序在制作表单的操作时使用 Request.RawUrl 属性。我确信这通常没问题,但它给我带来了问题。

我在我的网站上使用了 URL 重写器。在 global.asax 中,我有以下代码来理顺重写器的操作。

protected void Application_BeginRequest(object sender, EventArgs e)
{
  var app = sender as HttpApplication;
  if (app != null)
    if (app.Request.AppRelativeCurrentExecutionFilePath.Contains("~/rewritten.mvc"))
      app.Context.RewritePath(app.Request.Url.PathAndQuery.Replace("/rewritten.mvc", ""));
}

问题是 app.RewritePath 不影响 RawUrl 属性,因此当 BeginForm 助手写入操作时,它最终会在目标 url 中嵌入“/rewritten.mvc”,这是不正确的。

我知道我可以在 BeginForm() 方法中使用重载来专门针对 Controller 和操作,但这有其自身的缺点,在我的特定情况下我不能这样做。

在我的 Application_BeginRequest 方法中,如何改变 RawUrl 属性的输出? (我想我应该提到这个属性没有 setter。)

最佳答案

您无法更改 RawUrl 属性。您是否考虑过使用 MVC 中内置的路由功能而不是使用外部重写器?

关于c# - 如何更改 Request.RawUrl 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1160988/

相关文章:

asp.net-mvc - 在 .NET 中使用 FB Connect/Google OAuth 登录

c# - 如何在 LINQ 中分组然后选择元素到新类中(最好是 C#)

asp.net-mvc - 如何在 MVC 5 中使用 Visual Studio 2015 设置 angular-cli?

c# - Windows Phone 中的 MessageBoxButtons 成员

c# - 有没有一种方法可以使用 Visual Studio 2022 在编译时通过转换检查在 C# 中创建自定义整数类型?

asp.net-mvc - sitemap.xml url 返回 404 错误

javascript - 如何在 C# 服务器端处理从 jQuery POST 发送的参数?

asp.net-mvc - $http post 未与 asp.net MVC 模型绑定(bind)

c# - 完成队列释放速度不够快

c# - 在 Xamarin Forms 中使用网格显示项目