2016-12-28 5 views
0

을 :URL에서 액션 이름을 제거하고 URL에 페이지 제목을 추가 -이 같은 URL이 Asp.net MVC

http://localhost:17594/Contact/Contact 

지금 내가 이런 식으로 보여주고 싶은 :

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" } 
    ); 

} 

보기

하지만 633 번 노선에서 Categories 경로를 사용하는 중에 오류가 발생했습니다.

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")

무엇이 잘못 되었나요?

답변

0

이 당신의 경로 설정에서

파일을보십시오 :

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

당신은 두 가지 옵션이 있습니다.

어느

은 규칙 기반 라우팅

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" } 
); 

를 통해 특정 경로를 추가 또는 규칙 기반 경로

//enable attribute routing 
routes.MapMvcAttributeRoutes(); 

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

전에 RouteConfig에 라우팅 속성을 활성화하고 컨트롤러와 액션에 직접 경로를 적용합니다.

public class ContactController : Controller { 

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

}