Multiple routes assigned to one method, how to determine which route was called?

后端 未结 2 1487
耶瑟儿~
耶瑟儿~ 2021-01-12 00:00

I am working on a small ASP.NET MVC project at the moment. The project was released a few month ago. But changes should be implemented for usability and SEO reasons now. I d

2条回答
  •  盖世英雄少女心
    2021-01-12 00:21

    I wanted to be able to pass different views based on the request but they all basically used the same process and didn't want to make an action for each. The prior answer doesn't seem to work any more so here is what I came up with. This is .Net Core 2.2.

     [HttpGet]
    [Route("[controller]/ManageAccessView/{name}/{id}",Name = "ManageAccessView")]
    [Route("[controller]/ManageAccessUsers/{name}/{id}", Name = "ManageAccessUsers")]
    [Route("[controller]/ManageAccessKeys/{name}/{id}", Name = "ManageAccessKeys")]
    public async Task ManageAccessView(int id, string name)
    {
    
      var requestedView = this.ControllerContext.ActionDescriptor.AttributeRouteInfo.Name;
    
      return View(requestedView);
    
    
    }
    

    This will allow you to put your individual views as the name of the routes and use them to set the view.

提交回复
热议问题