Default Values ​​for MVC MVC

I defined two routes in global.asax as shown below

context.MapRoute("HomeRedirect", "",
                            new
                            {
                                controller = "Home",
                                action = "redirect"
                            });

context.MapRoute("UrlResolver", "{culture}/some",
                            new
                            {
                                culture = "en-gb",
                                controller = "someController",
                                action = "someAction"
                            },
                            new
                            {
                                culture = new CultureRouteConstraint()
                            });

in accordance with the above definition, when you need to call the user request mysite.com/ redirect actions of HomeController and in this:

public class HomeController : Controller
{
    public ActionResult Redirect()
    {
        return RedirectToRoute("UrlResolver");
    }
}

I want to redirect the user to the second specific route above, so I also specified default values ​​for this and some restrictions for each of them. but when RedirectToRoute("UrlResolver")rotated, the default values ​​passed to routeConstraints in the second route and No route in the route table matches the supplied valuesare not displayed.

Update

my CultureRouteConstraint:

public class CultureRouteConstraint : IRouteConstraint
{
    bool IRouteConstraint.Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        try
        {
            var parameter = values[parameterName] as string;
            return (someCondition(parameter));
        }
        catch
        {
            return false;
        }
    }
}

now the parameter value has no key / culture value, but the route parameter has this.

+3
source share
2

, , "{culture}/some", , , "/EN-us/some", .

+1

HomeController.Redirect(), , , HomeRedirect?

"UrlResolver" . , .

" " "UrlResolver", , CultureRouteConstraint() URL-.

+1

Source: https://habr.com/ru/post/1748957/


All Articles