开发者

Remove extra route value after route matching

开发者 https://www.devze.com 2023-03-23 10:07 出处:网络
Is there any way to remove some of route values after开发者_JAVA技巧 route was matched but before matched action is executed. My action doesn\'t contain some of route values and I want to remove them.

Is there any way to remove some of route values after开发者_JAVA技巧 route was matched but before matched action is executed. My action doesn't contain some of route values and I want to remove them.

In other words, there is some route value which won't be used in my controller (but is used by filters) and I don't want to keep it at action.


I have found an easy solution - define custom RouteHandler:

public class CustomMvcRouteHandler: MvcRouteHandler
{
    protected override System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        requestContext.RouteData.Values.Remove("someValue");
        return base.GetHttpHandler(requestContext);
    }
}

// ... and when defining the route, set the route handler to be your custom route handler
route.RouteHandler = new CustomMvcRouteHandler();


Anywhere you can get a hold of the RouteData for the request you can get call:

RouteData.Values.Remove("keyNameOfParameterToRemove");

But this comes with no promise of doing what you want it to do, unless you can expand on why you want to do this so I can test that specific goal.

0

精彩评论

暂无评论...
验证码 换一张
取 消