Avoid hard-coding controller and action names

前端 未结 5 800
攒了一身酷
攒了一身酷 2020-12-24 15:19

ASP.NET MVC seems to be encouraging me to use hard-coded strings to refer to controllers and actions.

For example, in a controller:

return RedirectT         


        
5条回答
  •  轮回少年
    2020-12-24 16:04

    I know this is an old topic, but when I was looking for an answer for ASP.NET 5 this theme first appeared. There is no need to hardcode anymore, just use nameof

    [HttpGet]
    public IActionResult List()
    {
       ...
       return View();
    }
    
    [HttpPost]
    public IActionResult Add()
    {
        ...
        return RedirectToAction(nameof(List));
    }
    

提交回复
热议问题