0

I have only route. It is configured like :

        routes.MapRoute(
            name: "Default",
            url: "{lang}/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", lang = "en", id = UrlParameter.Optional },
            constraints: new  { lang=new LanguageConstraint()
            }
        );

I'm creating link using

 <a href='@Url.Action("Index", "Results", new {     lang=System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName}')</a>

but link is not cerated at all. What I'm doing wrong? It seems fine. I have Index action and Results controller. It is ok when I have default route MVC comes with {controller}/{action}

This is LanguageConstraint:

      public class LanguageConstraint : IRouteConstraint
{
    public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (routeDirection == RouteDirection.IncomingRequest)
        {
            string language = values["lang"].ToString();  //(en|hr|de|it|fr|sk|nl|hu|sv|pl|cs|ru|sl
            if (language == "en" || language == "hr" || language == "de" || language == "it" || language == "fr" || language == "sk" || language == "nl" || 
                language == "hu" || language == "sv" || language == "pl" || language == "cs" || language == "ru" || language == "sl" )
                return true;
            else
                return false;
        }
        return false;
    }
}
7
  • What's the actual url that is being generated? Commented Nov 13, 2014 at 13:08
  • LanguageConstraint is you own class? Can you provide it in question? Commented Nov 13, 2014 at 13:16
  • couldn't it be just syntax error? try <a href='@Url.Action("Index", "Results", new { lang = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName })'></a> Commented Nov 13, 2014 at 13:22
  • If I comment out constaint and link is created. Commented Nov 13, 2014 at 13:22
  • and what is the value of language when you are in debug mode? Or it isn't go there? Commented Nov 13, 2014 at 13:26

1 Answer 1

1

You should change method like this i think

public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
    if (routeDirection == RouteDirection.IncomingRequest)
    {
        string language = values["lang"].ToString();  //(en|hr|de|it|fr|sk|nl|hu|sv|pl|cs|ru|sl
        if (language == "en" || language == "hr" || language == "de" || language == "it" || language == "fr" || language == "sk" || language == "nl" || 
            language == "hu" || language == "sv" || language == "pl" || language == "cs" || language == "ru" || language == "sl" )
            return true;
        else
            return false;
    }
    return true;
}

Problem with routeDirection when you create link it is RouteDirection.UrlGeneration

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.