Route to static pages in MVC

Asked

Viewed 1,139 times

1

I have a question about the application ASP.Net MVC:

Structure

Views
    |
    +-- Home
    |      |
    |      +-- Index.cshtml
    |      |
    |      +-- Page.cshtml
    |
    +-- Users
    |      |
    |      +-- Details.cshtml
    |
    +-- Paginas
    |      |
    |      +-- Pagina1.cshtml
    |      |
    |      +-- Pagina2.cshtml
    |
    +-- Shared
             |
             +-- Layout.cshtml
             |
             +-- _Partial.cshtml

A normal structure, but the folder "Pages" has no controller that makes the association. In this folder I’ll put all the pages that are "Html Statics". That is if I create a new file "Pagina3.cshtml" and put in this folder, and when I access the url {local}\paginas\Pagina3 the contents of this page appear, but when I do this returns a 404 error.

I just don’t want to create files .html by using the Layout.cshtml.

  • Create a standard controller to handle these static files

  • Use pages really static (HTML even) is out of the question?

  • @Andrecalil the problem with using static pages is that I won’t be able to use Layout.cshtml

  • @Tiago: Use the method HandleUnknownAction as I indicated in my reply... this method exists for this purpose.

3 answers

1

For the route to work properly, you would need to have a PaginasController, each method of this controller pointing to a page in your Views:

namespace SeuProjeto.Controllers
{
    public class PaginasController : Controller
    {
        public ViewResult Pagina1() {
            Return View();
        }

        public ViewResult Pagina2() {
            Return View();
        }
    }
}
  • Okay, but every time I create a new page, I have to create a new method too. I did not want to perform this procedure, I would just like to add these content pages, without having to change some controller

  • There’s no way. That’s not how MVC works.

  • If the page is static, do not need a method for each no, just create a url vs page map to be shown and create a single controller, as StaticController with a method ViewResult servStatic(String viewName)

1


According to the structure you sent, you will need to do a subrouting in a controller "Pages".

The "id" parameter is required if you do not want to change the default route: {controller}/{action}/{id}

namespace MyDynamicRoutingApp.Controllers
{
    public class PaginasController : Controller
    {
        //
        // GET: /Paginas/pagina/{id}
        // Exemplo: /Paginas/pagina/pagina1
        // Exemplo: /Paginas/pagina/pagina2....
        public ActionResult Pagina(string id)
        {
            return View(id);
        }
    }
}
  • Alan worked, I just changed the name of the method from Pages to Page...

  • It is only interesting you also do a treatment and return 404 if the requested page does not exist.

0

The way to do this is by using the method virtual HandleUnknownAction class Controller from which all controlers derive. The following example renders any page whose cshtml exists in the view folder corresponding to the controller:

public class StaticController : Controller
{
    protected override void HandleUnknownAction(string actionName)
    {
        try
        {
            this.View(actionName).ExecuteResult(this.ControllerContext);
        }
        catch
        {
            this.HttpNotFound("A página requisitada não existe.")
                .ExecuteResult(this.ControllerContext);
        }
    }
}

Now, any views you create inside the folder \View\Static\ will be rendered when calling the controller, with any action name... and if the view does not exist, a 404 will be returned.

You can use all the features in the views, Layout page, you can use a model (as long as you pass one in the controller method).

Action QualquerPagina, que não existe no controller

Note that the actions Index and QualquerPagina are not stated in StaticController, and yet they will work.

P.S. just so you know, my route configuration is like this:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Home",
    url: "{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.