Wednesday, 15 July 2015

asp.net mvc - RequestContext - RouteData does not contain action -


That's why I've created Factory own controller and I'm overloaded GetControllerSessionBehavior to increase MVC behavior.

My custom work will have to use the reflection on the action called to me. Although I stumbled on a strange issue - I can not retrieve the action by accessing RequestContext. RouteData

While setting a breeding sample for it I was not able to reproduce the error.

Is anyone aware of the possible reasons for this, or in some other way, how to get the action by calling a method with the request reference?

  public class CustomControllerFactory: DefaultControllerFactory {protected override session Steshivier GetControllerSessionBehavior (RequestContext Anurodcontekst type controller type) {if (request! Var controller action = requestContext.RouteData.Values ​​[ "action"]; Var action = controller action. ToString (); Var action method type = controller. GETMember (action, member type. method, Baindingflagkinstens | Baindingflag. public). Frstor default (); if (action method = = R Person) return to base. GetControllerSessionBehavior (requestContext, controller type); Var cattr = actionMethod.GetCustomAttribute & lt; Satrstetakshnattivet & gt; (); if (cattr! = Null) return cattr. Word; Return Adharkgetkantrolr Satrbavik ( Request Context, Controller Type);}}  

The action that I can just say right but my controller can not access the action name within the factory:

  [Route ("Open / {Createmodel: bool?} / {TlpId: int} / {siteId: int?}")] Public ActionSultal Open ( At TLPID, INT SIDID, BULL? Vsmodel = true) {}  

Any ideas welcome.

Update:

The problem is related to the routing attribute. It is working properly, but it does not work in production for me.

On the way it got - once the answer is given, I feel that I will also have the proper solution.

Update 2:

Interesting breeding mcc version 5.0.0.0, production 5.2.2. Probable start of bug?

I can confirm that 5.0.0 and 5.1.1 I have reported this issue, however, in my use Microsoft was able to provide an acceptable solution.

in

On the other hand, the problem you look like a criminal to feature routing nested named root key MS_DirectRouteMatches Is stored. I'm not sure which version has actually changed, but I know that this v5 + happened.

So, to fix your problem, you will need to check the existence of the nested Rutadata collection, and instead use common case in the case.

  varDataData = requestContext.RouteData; If (Margdetakvolukkantenski ( "MS_DirectRouteMatches")) {Margdeta = ((Aianimrabl & lt; Rutdeta & gt;) Margdeta. Value [ "MS_DirectRouteMatches"]). first (); } Var Controller Action = Route Data Value ["action"]; Var Action = Controller Action. ToString ();  

BTW - In the provided by you, the asker had assumed that there is a possibility where a request can match more than one route. But this is not possible - a request will match 0 or 1 route, but will never be more than one.


No comments:

Post a Comment