mccalltd / AttributeRouting

Define your routes using attributes on actions in ASP.NET MVC and Web API.
http://mccalltd.github.io/AttributeRouting/
MIT License
416 stars 89 forks source link

Get Attribute Routing Route Url in View #243

Open mdmoura opened 11 years ago

mdmoura commented 11 years ago

Hello,

I have the following AR:

namespace MvcSite.Areas.Cms.Controllers.Api {
  [RouteArea("Cms", AreaUrl = "{culture}/Cms"), RoutePrefix("Api/Access")]
  public class UserController : ApiController {
    [POST("Users/{slug}/Unlock"), HttpPost]
    public String Unlock(String slug) { }
   }
}

I checked routes.axd and I have:

METHODS: POST, OPTIONS
URL: {culture}/cms/api/access/users/{slug}/unlock
DEFAULTS: controller: User, action: Unlock CONTRAINTS: inboundHttpMethod: IRouteConstraintProxy_3

DATA TOKENS: namespaces: MvcSite.Areas.Cms.Controllers.Api defaultSubdomain: www area: Cms UseNamespaceFallback: False

I have two UserController (One API one normal) in the following namespaces: MvcSite.Areas.Cms.Controllers and MvcSite.Areas.Cms.Controllers.Api.

I am trying, on a view, to get the url of the unlock action: @Url.Action("unlock", "user", new { area = "cms", slug = "johnslug" })

The expected route should be: "/en/cms/api/access/users/johnslug/unlock"

But I am not able to get it ...

I am not sure if the AR AreaUrl and RoutePrefix are a problem in this.

Is it possible to get the route?

Thank You, Miguel

mccalltd commented 11 years ago

Need culture route val.

On Apr 17, 2013, at 8:02 AM, Miguel Moura notifications@github.com wrote:

Hello,

I have the following AR:

namespace MvcSite.Areas.Cms.Controllers.Api { [RouteArea("Cms", AreaUrl = "{culture}/Cms"), RoutePrefix("Api/Access")] public class UserController : ApiController { [POST("Users/{slug}/Unlock"), HttpPost] public String Unlock(String slug) { } } }

I checked routes.axd and I have:

METHODS: POST, OPTIONS

URL: {culture}/cms/api/access/users/{slug}/unlock

DEFAULTS: controller: User, action: Unlock CONTRAINTS: inboundHttpMethod: IRouteConstraintProxy_3

DATA TOKENS: namespaces: MvcSite.Areas.Cms.Controllers.Api defaultSubdomain: www area: Cms UseNamespaceFallback: False

I have two UserController (One API one normal) in the following namespaces: MvcSite.Areas.Cms.Controllers and MvcSite.Areas.Cms.Controllers.Api.

I am trying, on a view, to get the url of the unlock action: @Url.Action("unlock", "user", new { area = "cms", slug = "johnslug" })

The expected route should be: "/en/cms/api/access/users/johnslug/unlock"

But I am not able to get it ...

I am not sure if the AR AreaUrl and RoutePrefix are a problem in this.

Is it possible to get the route?

Thank You, Miguel

— Reply to this email directly or view it on GitHubhttps://github.com/mccalltd/AttributeRouting/issues/243 .

mdmoura commented 11 years ago

Yes,

I also tried:

@Url.Action("unlock", "user", new { area = "cms", culture = "en", slug = "johnus" })

Or:

@Url.Action("unlock", "user", new { area = "cms", culture = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName, slug = "johnus" })

But I get:

/cms/user/unlock?culture=en&slug=johnus

Shouldn't I get an exception instead of the malformed route?

mdmoura commented 11 years ago

By the way, my routing configuration is:

  RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  RouteTable.Routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" });

  RouteTable.Routes.MapAttributeRoutes(x => {
    x.AddRoutesFromAssemblyOf<HomeController>();
    x.ConstrainTranslatedRoutesByCurrentUICulture = true;
    x.InlineRouteConstraints.Add("notetype", typeof(EnumValueRouteConstraint<NoteType>));
    x.UseLowercaseRoutes = true;
    x.UseRouteHandler(() => new CultureRouteHandler());
    x.CurrentUICultureResolver = (context, data) => { return (String)data.Values["culture"] ?? Thread.CurrentThread.CurrentUICulture.Name; };
  });

  GlobalConfiguration.Configuration.Routes.MapHttpAttributeRoutes(x => {
    x.AddRoutesFromAssemblyOf<HomeController>();
    x.ConstrainTranslatedRoutesByCurrentUICulture = true;
    x.InlineRouteConstraints.Add("notetype", typeof(EnumValueRouteConstraint<NoteType>));
    x.UseLowercaseRoutes = true;
    x.UseRouteHandler(() => new CultureRouteHandler());
    x.CurrentUICultureResolver = (context, data) => { return (String)data.Values["culture"] ?? Thread.CurrentThread.CurrentUICulture.Name; };
  });

  AreaRegistration.RegisterAllAreas();

  RouteTable.Routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new String[] { typeof(HomeController).Namespace });

  RouteTable.Routes.MapHttpRoute("Default_Api", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });

And my CMS route configuration is:

context.MapRoute("Cms_Default", "Cms/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional }, new String[] { typeof(MvcSite.Areas.Cms.Controllers.HomeController).Namespace });

Thank You