Rota Dynamica ActionResult

1

I have two Controllers: Faculty and Course

public class InstituicaoController : Controller
    {
        // GET: Instituicao
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Instituicao(string instituicao) {

            return View();

        }

        public ActionResult Curso() {

            return View();
        }
    }


<a href="@Url.Action("Curso", "?", new {faculdade = "NomeFaculdade", curso = "Nomecurso"})">Detalhe do Curso </a>

What is the procedure to leave this way: www.example.com.br/{Name of Faculty Faculty} / {Name of Course}

    
asked by anonymous 15.12.2015 / 21:09

1 answer

0

Register the following route in RouteConfig.cs :

        routes.MapRoute(
            name: "Instituicao",
            url: "{faculdade}/{curso}",
            defaults: new { controller = "Instituicao", action = "Instituicao", faculdade = "", curso = "" }
        );

We will have to exclude% of% of default routes because the Default route is greedy and will attempt to resolve to Instituicao as well. Implement the following attribute:

public class NotEqual : IRouteConstraint
{
    private string _match = String.Empty;

    public NotEqual(string match)
    {
        _match = match;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        return String.Compare(values[parameterName].ToString(), _match, true) != 0;
    }
}

Modify the Default route to:

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