6
url과 같은 stackoverflow를 생성하려고합니다.MVC 4 슬러그 타입 URL 생성
다음 예제는 잘 동작합니다. 하지만 컨트롤러를 제거하면 오류가 발생합니다.
http://localhost:12719/Thread/Thread/500/slug-url-text
첫 번째 스레드는 두 번째 동작 인 컨트롤러입니다.
URL에서 컨트롤러 이름을 제외하고 위와 같은 URL을 어떻게 만들 수 있습니까? 기본 경로를 정의하기 전에 다음과 같은 경로를 배치
http://localhost:12719/Thread/500/slug-url-text
내 경로
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}/{ignoreThisBit}",
new
{
controller = "Home",
action = "Index",
id = "",
ignoreThisBit = ""
}); // Parameter defaults)
}
}
스레드 컨트롤러
public class ThreadController : Controller
{
//
// GET: /Thread/
public ActionResult Index()
{
string s = URLFriendly("slug-url-text");
string url = "Thread/" + 500 + "/" + s;
return RedirectPermanent(url);
}
public ActionResult Thread(int id, string slug)
{
return View("Index");
}
}
더 나은 문자열 검사를 위해 string.IsNullOrWhiteSpace로 string.IsNullOrEmpty를 변경하십시오. –