3 가지 경로와 적절한 RouteConstraints
으로 원하는 것을 얻을 수 있습니다.
첫째, 당신은 당신의 경로를 설정해야합니다 :
using System;
using System.Web;
using System.Web.Routing;
namespace ExampleApp.Extensions
{
public class MustBeValidAuthor : IRouteConstraint
{
public MustBeValidAuthor() { }
private DbContext _db = new DbContext();
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return (_db.Authors.Where(u => u.AuthorName == values[parameterName].ToString()).Count() > 0);
}
}
public class MustBeValidDocument : IRouteConstraint
{
public MustBeValidDocument() { }
private DbContext _db = new DbContext();
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return (_db.Documents.Where(u => u.DocumentName == values[parameterName].ToString()).Count() > 0);
}
}
}
RouteConstraints는 enfoce됩니다
이
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Author_Document", // Route name
"{author}/{document}", // URL
new { controller = "ShowDocument", action = "AuthorDocument" }, // Parameters
new { author = new MustBeValidAuthor(), document = new MustBeValidDocument() }
routes.MapRoute(
"Author", // Route name
"{author}", // URL
new { controller = "ShowDocument", action = "Author" }, // Parameters
new { author = new MustBeValidAuthor() }
routes.MapRoute(
"Document", // Route name
"{document}", // URL
new { controller = "ShowDocument", action = "Document" }, // Parameters
new { document = new MustBeValidDocument() }
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
다음, 당신이 제공 한 정보가 유효한지 확인해야하는 당신의 RouteConstraints을 작성해야 요청의 유효성 및이 순서로 라우트를 배치하는 것은 요구 사항을 충족시킵니다.
문서의 작성자와 동일한 ID를 사용할 수 없습니까? – joshb
'document (id)'의'id'가 정수인 곳을 의미합니까? URL은 컨트롤러 및 액션에 매핑하는 방법을 보여줍니다. – TheCodeKing
@ joshb : 사실은 아니지만 저자가 우선해야합니다. I.E 저자 djquery 및 문서 djquery는/djquery/djquery/ – David