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()方法中使用重载来专门针对控制器和动作,但这有其自身的缺点,在我的特定情况下我不能这样做。
在我的Application_BeginRequest方法中,可以做些什么来改变RawUrl属性的输出? (我想我应该提一下,这个属性没有setter。)
答案 0 :(得分:1)
您无法更改RawUrl属性。您是否考虑过使用MVC内置的路由功能而不是使用外部重写器?