Customize aspnet core routing attribute so that Url.Action() returns a different url?

63 Views Asked by At

This is an example of what I want to achieve, however I want to do my own custom attribute that also feeds itself from something other than the request url. In the case of HttpGet/HttpPost these built-in attributes obviously have to look at the http request method, but is there truly no way to make Url.Action() resolve the correct url then?

    [HttpGet("mygeturl")]
    [HttpPost("myposturl")]
    public ActionResult IndexAsync()
    {
        // correct result: I get '/mygeturl' back
        var getUrl = Url.Action("Index");

        // wrong result: It adds a ?method=POST query param instead of returning '/myposturl'
        var postUrl = Url.Action("Index", new { method = "POST" }); 

        return View();
    }

I've looked at the aspnet core source code and I truly can't find a feature that would work here. All the LinkGenerator source code seems to require routedata values but routedata always seems to require to be in the url somewhere, either in the path or in the query string. But even if I add the routedata value programmatically, it won't be in time for the action selection or the linkgenerator doesn't care. In theory what I need is to pass something to the UrlHelper/LinkGenerator and have it understand that I want the url back out that I defined in my custom attribute, in this case the HttpPost (but I'll make my own attribute).

0

There are 0 best solutions below