2016-12-28 1 views
0

J'ai une URL comme ceci:Supprimer le nom d'action de l'URL et ajouter la page titre à l'URL - Asp.net MVC

http://localhost:17594/Contact/Contact 

Maintenant, je veux montrer comme ceci:

http://localhost:17594/Contact/Contact-us 

RouteConfig :

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute(
     name: "Categories", 
     url: "Categories/{id}", 
     defaults: new { controller = "Categories", action = "Index", id = UrlParameter.Optional }, 
     namespaces: new[] { "FinalKaminet.Controllers" } 
    ); 

    routes.MapRoute(
     name: "Contacts", 
     url: "{controller}/{title}", 
     defaults: new { controller = "Contact", action = "Contact", title = UrlParameter.Optional }, 
     namespaces: new[] { "FinalKaminet.Controllers" } 
    ); 

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

} 

Voir

@Html.ActionLink("Contact Us", "Contact" , "Contact" , new { title = "contact-us" } , null) 

Mais j'ai une erreur dans la ligne 63 qui utilise Categories carte route.

Exception Details: System.InvalidOperationException: No route in the route table matches the supplied values.

Source Error:

Line 62: @Html.ActionLink("وبلاگ", "")

Line 63: @Html.Action("MenuCat" , "Home")

Qu'est-ce qui ne va pas?

Répondre

0

Essayez cette

Dans votre fichier de configuration de la route :

routes.MapRoute(
     name: "Contacts", 
     url: "Contact/{action}/{title}", 
     defaults: new { controller = "Contact", action = "Contact", title = UrlParameter.Optional }, 
     namespaces: new[] { "FinalKaminet.Controllers" } 
    ); 
0

Vous avez deux options.

Soit ajouter un itinéraire spécifique via le routage basé sur la convention-

routes.MapRoute(
    name: "ContactUs", 
    url: "contact/contact-us", 
    defaults: new { controller = "Contact", action = "Contact" }, 
    namespaces: new[] { "FinalKaminet.Controllers" } 
); 

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

ou activer l'attribut de routage dans RouteConfig avant les routes de type conventionnel

//enable attribute routing 
routes.MapMvcAttributeRoutes(); 

//other convention-based routes. 
routes.MapRoute(....); 

et appliquent la route directement au contrôleur et à l'action.

public class ContactController : Controller { 

    //GET contact/contact-us 
    [HttpGet] 
    [Route("Contact/Contact-us")] 
    public ActionResult Contact() { … } 

}