I have one controller HomeController.cs
.
In this controller I have some actions like about,contact etc.
If I want that will work I have to defined function to each view:
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
I want that for all the requests to the controller it will be return the View()
, so I will not have to defined a function to each view.
*Update I want to know if there is some handler that will work something like this:
protected override void OnActionCall()
{
return View();
}
WARNING: I would strongly advise against this as it pretty much defeats the purpose of using MVC but...
You could create a general action that will find the view for you:
public ViewResult Page(string urlSlug)
{
if (!this.ViewExists(urlSlug))
{
throw new HttpException(404, "Page not found");
}
return this.View(urlSlug);
}
private bool ViewExists(string name)
{
ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null);
return (result.View != null);
}
You will need to edit your RouteConfig
though:
routes.MapRoute(
name: "Home_Page",
url: "{urlSlug}",
defaults: new { controller = "Home", action = "Page" },
constraints: new { urlSlug = @"[A-Za-z0-9-]{1,50}" }
);
Now if you look for /about
it will look for that view. You will run into problems when you want to use Model
data though. I think the time spent asking this question and implementing this solution would take a lot longer than simply having return View()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.