-1

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();
    }
12
  • What's the point here? This goes against the MVC structure principles. Commented Mar 11, 2015 at 10:27
  • 2
    So you're not being very clear on what you're trying to achieve. Please elaborate. Commented Mar 11, 2015 at 10:29
  • 1
    What you're doing is exactly that. Responding to an action by presenting a view, via the controller. What's to change here? Commented Mar 11, 2015 at 10:34
  • 1
    How can you expect to have an orphan action with nothing on the controller to respond to? Commented Mar 11, 2015 at 10:37
  • 1
    You mean to say, if you're not doing anything in your action method except that single return statement, then you don't want to have that action method in your controller, is it? Commented Mar 11, 2015 at 10:37

1 Answer 1

3

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()

Sign up to request clarification or add additional context in comments.

5 Comments

while this surely works, this is the kind of thing that goes against MVC conventions, and will be very hard to troubleshoot and maintain.
@Claies I wholeheartedly agree - I will put a warning at the top of my answer.
Honestly, I do not like that people use this argument. This is true, but sometimes this kind of solutions actually prevent problems. I think that the idea of the code is to find common ground, and this solution really find common ground. I would prefer to have a built-in .NET solution of course.
@mosh If you want a bunch of static pages...why not use static pages?
@jumpingcode I want to use other aspects of MVC like templates routs and other staff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.