Mount URL without displaying the Action or Controller name or both

3

Anyone who has experience in e- commerce knows what I'm going to ask. Usually, when we create a route, and call our Action related to that route, the URL is mounted like this:

link

If there are parameters, then it is loaded after the Action name. What I'm needing is to know if there's any way I can not show the Controller name (in my example, Home) and neither the Action name, just a parameter, which would be the name of the product, as with e- commerce , like this:

link

That is, after the bar, only show the parameters of the route. Is it like, using MVC 5?

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

    
asked by anonymous 19.03.2014 / 17:12

1 answer

5

This description after the address is called slug . In this case, what you need to do is a specific route manipulator. For example:

AppStart \ RouteConfig.cs

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

Infrastructure \ MyRotate Handler

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 \ ArticlePage.cs

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

Infrastructure \ Redirection Manager.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;
    }
}
    
19.03.2014 / 17:26