shkipper
shkipper

Reputation: 1413

AttributeRouting - area with optional route parameter

is it possible to have area with two routes. for example:

[RouteArea("{culture}/testarea")] <-- specific culture
[RouteArea("testarea")]  <-- default culture
LocalizableAreaBaseController ....

Thank You!

Upvotes: 1

Views: 932

Answers (2)

SimonOzturk
SimonOzturk

Reputation: 791

There is another prefix for that in RouteArea attribute please use that. That will works.

[RouteArea("testarea"),AreaPrefix="{culture}/testarea"] <-- specific culture

Upvotes: 1

Jeremy Bell
Jeremy Bell

Reputation: 718

You could accomplish this with a route constraint.

public class TestAreaAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "TestArea";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "TestArea_culture",
            "{culture}/TestArea/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            new { culture = @"^[A-Za-z]{2}(\-[A-Za-z]{2})$"}
        );

        context.MapRoute(
            "TestArea_default",
            "TestArea/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}

Or

[RoutePrefix("{culture:regex(^[A-Za-z]{2}(\-[A-Za-z]{2})$)}/TestArea")]
[RoutePrefix("TestArea")]
public class TestController : ApiController {
}

(This particular regex constraint would match something like "en" or "en-gb")

Upvotes: 1

Related Questions