Mount URL without the Action or Controller name or both

Asked

Viewed 1,766 times

4

Who has experience in and-Commerce, you know what I’m going to ask. Normally, when we create a route, and we call our Action related to that route, the URL is mounted like this:

http://minha_pagina.com.br/Home/Minha_Action/

If there are parameters, then it is loaded after the Action name. What I need is to know if there is a way to not show the name of the Controller (in my example, Home) and not the name of Action, just a parameter, which would be the name of the product, as with and-Commerce, kind of:

http://www.e-commerce.com.br/tv-samsung-52-ultra-slin-cor-preta

That is, after the slider, show only the route parameters. It has as, using MVC 5?

If it is not possible, give live with the name of the controller, but Action would like to remove.

  • Do you know how the site works? Please indicate when to find an answer that solves your problem by marking it as a solution (by clicking on the "right" icon just below the answer score)

1 answer

6

This description that stands after the address is called Slug. In this case, what you need to do is a specific route handler. For example:

Appstart Routeconfig.Cs

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Default", action = "Index", id = UrlParameter.Optional }
).RouteHandler = new MeuManipuladorDeRota();

Infrastructure Meumanipuladorderota.Cs

public class MeuManipuladorDeRota : MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var url = requestContext.HttpContext.Request.Path.TrimStart('/');

        if (!string.IsNullOrEmpty(url))
        {
            ItemDePagina item = GerenciadorDeRedirecionamento.ObterPaginaPorUrl(url);
            if (item != null)
            {
                MontarRequisicao(item.Controller, 
                    item.Action ?? "Index", 
                    item.ConteudoId .ToString(), 
                    requestContext);
            }
        }

        return base.GetHttpHandler(requestContext);
    }

    private static void MontarRequisicao(string controller, string action, string id, RequestContext requestContext)
    {
        if (requestContext == null)
        {
            throw new ArgumentNullException("requestContext");
        }

        requestContext.RouteData.Values["controller"] = controller;
        requestContext.RouteData.Values["action"] = action;
        requestContext.RouteData.Values["id"] = id;
    }
}

Viewmodels Itemdepagina.Cs

public class ItemDePagina {
    public String Controller { get; set; }
    public String Action { get; set; }
    public int ConteudoId { get; set; }
}

Managed Infrastructure redirection.Cs

public static class GerenciadorDeRedirecionamento
{
    public static ItemDePagina ObterPaginaPorUrl(string url)
    {
        ItemDePagina item = null;

        /* Aqui você pesquisa na entidade pela descrição, passando o parâmetro url. */
        /* Este é o ponto mais importante da lógica, que é onde você vai pesquisar o item de acordo com as suas regras de negócio. */
        /* Depois você monta um objeto ItemDePagina (no caso, item) e o devolve. */

        return item;
    }
}

Browser other questions tagged

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