开发者

Question about RouteData and ValueProvider on Asp.net MVC3

i have one doubt

To use this sentence: filterContext.RouteData.Values["MyRouteValue"] the key should be declared in a Route in the route table right?

I mean, should be in the route table something like: {MyRouteValue}/

If i have a request lik开发者_运维百科e /Controller/Action?MyRouteValue=XXXXXXX im getting null from filterContext.RouteData.Values["MyRouteValue"]. This means that if i want to get the value i have to use filterContext.Controller.ValueProvider.GetValue("publicationUrlTitle").AttemptedValue istead?

Thanks in advance.

Regards.

Jose.


To use this sentence: filterContext.RouteData.Values["MyRouteValue"] the key should be declared in a Route in the route table right?

Yes.

If i have a request like /Controller/Action?MyRouteValue=XXXXXXX im getting null from filterContext.RouteData.Values["MyRouteValue"]. This means that if i want to get the value i have to use filterContext.Controller.ValueProvider.GetValue("publicationUrlTitle").AttemptedValue istead?

Yes, use ValueProvider or read it from filterContext.HttpContext.Request["MyRouteValue"]. The advantage of using ValueProvider is that it looks in many places: route data, GET query string parameters, POST parameters, custom value providers such as a JSON value provider in ASP.NET MVC 3, ...

Just be careful by when you access the .AttemptedValue property as you might get a NRE if the MyRouteValue is nowhere to be found.


In order to debug all routeValues, I've made a filter that copies all action parameters from the action method to the routedata values table:

public class CopyActionParametersToRouteData : ActionFilterAttribute {
    public override void OnActionExecuting(ActionExecutingContext filterContext) {
        var controller = filterContext.Controller as Controller;
        if (controller == null)
            return;

        foreach (var actionParameter in filterContext.ActionParameters) {
            if (controller.RouteData.Values.ContainsKey(actionParameter.Key))
                continue;
            controller.RouteData.Values.Add(actionParameter.Key, actionParameter.Value);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜