tags:

views:

209

answers:

1

MVC's BeginForm helper uses the Request.RawUrl property when crafting a form's action. I'm sure this is generally fine but it is causing an issue for me.

I use a URL rewriter on my site. In the global.asax I have the following code to straighten out the rewriter's actions.

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", ""));
}

The problem is that the app.RewritePath does not affect the RawUrl property so when the BeginForm helper writes the action it ends up with the "/rewritten.mvc" embedded in the target url which is incorrect.

I know I can use an overload in the BeginForm() method to specifically target a controller and action but this has its own drawbacks, and in my particular case I cannot do that.

In my Application_BeginRequest method, what can do to alter the output of the RawUrl property? (I guess I should mention that this property doesn't have a setter.)

A: 

You can't change the RawUrl property. Have you looked at using the routing functionality built into MVC instead of using an external re-writer?

Eric J. Smith