Name the view / action "View" in ASP.NET MVC without changing routes

Can I name a view / action "View" in ASP.NET MVC without creating an additional set of route definitions? I was thinking about attributes, but could not find the right one. "View" as a function name is not possible because it hides the method of the deriven class Controller.

An example of what I mean with the view:

public ActionResult HereShouldBeViewInsteadOfThisText()
{
    return View();
}

Therefore, I can use url /home/view/id.

+4
source share
2 answers

You can use the attribute ActionNamefor the action method. as

[ActionName("View")]
public ActionResult ViewSomething(string id)
{
    return View();
}

This workaround is mentioned in this Phil Haake post

, .

+2

. MVC . , :

public ActionResult MyAwesomeAction()
{
    return View("View");
}

, , - . , , MVC, " Foo, Foo.cshtml". , .

+1

Source: https://habr.com/ru/post/1541070/


All Articles