0

I need to have a variable before every controller.

I want something like that: www.mysite.com/SYSTEMVARIABLE/Controller/Action

Example:

www.mysite.com/internalevent/Inscricao/Index

www.mysite.com/externalevent/Inscricao/Index

And i need to get the "internalevent" or "externalevent"

Example of values array At this example, i need the [0] value of array, be the SYSTEMVARIABLE, not "Inscricao (Controller)"

So, how can i get the "SYSTEMVARIABLE" value and work with this ?

I have the following now (not working...)

        routes.MapRoute(
            name: "Default",
            url: "{sistema}/{controller}/{action}/{id}",
            defaults: new { controller = "Inscricao", action = "Index", id = UrlParameter.Optional },
            constraints: new { sistema = new SistemaRouteConstraint() }
        );

I need to get the value from "{sistema}", and i need to do some logic on this value.

Below is the class i want to get it, but ALWAYS the "{sistema}" only gets me the controller value (controller come twice on RouteValueDictionary)

public class SistemaRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        object sistema;
        if(values.TryGetValue(parameterName, out sistema) && values != null)
        {
            var NomeSistema = values["sistema"].ToString();
            using (Entities db = new Entities())
            {                    
                /*some logic here*/
            }
        }

        return true;
    }
}
iganja
  • 19
  • 7
  • What exact value is in the url for `sistema`? – Chetan Nov 12 '19 at 00:16
  • this: www.mysite.com/internalevent/Controller/Action or www.mysite.com/externalevent/Controller/Action. It should be a string. I need to get the "internalevent" or "externalevent" value – iganja Nov 12 '19 at 00:18
  • You have access to `route` in Match method. route has Constraints property. You can use it to get the sistema constraint and it's value from it. – Chetan Nov 12 '19 at 00:24
  • not really, if i use `route.Constraints.TryGetValue("sistema", out sistema)`, it gets me only "**true**" value. and if i try to use `route.Constraints["sistema"].ToString()`, it gives me "**CustomAttributes.SistemaRouteConstraint**", not a valid value also – iganja Nov 12 '19 at 00:31
  • Can you share the code of `SistemaRouteConstraint`? Where the Match method is defined? How is it being used? – Chetan Nov 12 '19 at 01:07
  • I just edited my question with the `SistemaRouteContraint`. Its exactly where Match method is defined. Btw i dont know if the `IRouteConstraint` is the best way to get this value... i just need to get it all requests – iganja Nov 12 '19 at 01:09
  • it can be help https://stackoverflow.com/questions/36996021/how-to-change-route-to-username-after-logged-in – erdi yılmaz Nov 12 '19 at 05:12
  • 1
    Thanks @erdiyılmaz ! It worked – iganja Nov 13 '19 at 23:44

0 Answers0