Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Consider allowing RouteDataActionConstraintAttribute on methods #676

Closed
rynowak opened this issue Jun 18, 2014 · 7 comments
Closed

Consider allowing RouteDataActionConstraintAttribute on methods #676

rynowak opened this issue Jun 18, 2014 · 7 comments

Comments

@rynowak
Copy link
Member

rynowak commented Jun 18, 2014

Currently we're only looking for RouteDataActionConstraintAttribute on classes - which is needed for area support. We should consider looking at actions as well for conceptual symmetry.

AreaAttribute would still only apply to classes.

@rynowak
Copy link
Member Author

rynowak commented Aug 14, 2014

The attribute is marked as applying to classes and methods, but currently we only look for it on classes.

An attribute present on an action with the same route data key should override the value on the controller.

@anfomin
Copy link

anfomin commented Sep 14, 2014

I've ran this bug right now. Any news when you are going to resolve it?

@rynowak
Copy link
Member Author

rynowak commented Sep 15, 2014

@anfomin I'm curious... what are you trying to use it for? We haven't talked/blogged about this feature much so I'm really interested to know what you're trying to use it for. This will help us prioritize/trigae

@anfomin
Copy link

anfomin commented Sep 15, 2014

I'm writing action with paging support and want 2 routes:

  1. Route for first page: /path
  2. Route for other pages: /path/page{page:int:min(2)}

There is a controller:

[Route("Path")]
public class MySuperController : Controller
{
    [HttpGet, DenyPage]
    public IActionResult Index()
    {
        return Index(1);
    }

    [HttpGet("Page{page:int:min(2)}")]
    public IActionResult Index(int page)
    {
        // some code
    }
}

I've created DenyPageAttribute to block page parameter for first action:

public class DenyPageAttribute : RouteConstraintAttribute
{
    public DenyPageAttribute(string key = "page")
        : base(key, RouteKeyHandling.DenyKey)
    {
    }
}

But DenyPageAttribute useless until RouteConstraintAttribute will work for action-method. So if I write in view:

Url.Action("Index", new { page = 2 })

Current result: /Path?page=2
Expected result: /Path/Page2

@rynowak
Copy link
Member Author

rynowak commented Sep 15, 2014

Pretty cool, this is basically what the feature is for - we use it implement areas but it fits what you're doing as well. @yishaigalatzer thoughts on priority for this? it's a small item

What you're trying will certainly work (assuming we complete this item), but there are also a few simple things you try as workarounds.

  • Use Order - set Order = 1 on your Index() action then it will have lower priority than the other overload
  • Use named routes
  • Rename one of your actions - If you're using attribute routing, there's nothing particularly meaningful about the action names, it's just there for link generation.

@anfomin
Copy link

anfomin commented Sep 16, 2014

Thanks for workaround. I've done via Order = 1:

[Route("Path")]
public class MySuperController : Controller
{
    [HttpGet(Order = 1)]
    public IActionResult Index()
    {
        return Index(1);
    }

    [HttpGet("Page{page:int:min(2)}")]
    public IActionResult Index(int page)
    {
        // some code
    }
}

In this code I can't use named routes or another action name because it's required to create URL to the next page without knowing actions. For example, next page URL will be generated with:

Url.Action(null, new { page = page + 1 });

@danroth27 danroth27 added this to the 6.0.0-rc1 milestone Oct 15, 2014
@rynowak
Copy link
Member Author

rynowak commented Dec 31, 2014

ae9fc79

@rynowak rynowak closed this as completed Dec 31, 2014
@rynowak rynowak self-assigned this Dec 31, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants