Route for static pages in MVC

1

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

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 "Pages" folder does not have any 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 "Page3.cshtml" and put in this folder, and when I access url {local}\paginas\Pagina3 the contents of this page appear, but when I do this it returns a 404 error.

I just do not want to create .html files because I use Layout.cshtml .

    
asked by anonymous 22.02.2014 / 16:24

3 answers

1

According to the structure you have submitted, you will need to sub-routing a "Pages" controller.

  

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);
        }
    }
}
    
22.02.2014 / 22:56
1

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

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

        public ViewResult Pagina2() {
            Return View();
        }
    }
}
    
22.02.2014 / 20:40
0

The way to do this is by using the virtual HandleUnknownAction " of class Controller from which all controlers derive. The following example renders any page whose cshtml exists in the views 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 within the \View\Static\ folder 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 (provided you pass one in the controller method).

NotethatactionsIndexandQualquerPaginaarenotdeclaredinStaticController,andwillstillwork.

P.S.Justtostate,myrouteconfigurationlookslikethis:

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 }
);
    
23.02.2014 / 19:15