당신의 Web.config 또는 데이터베이스에서 값을 얻기를 요구하고 있는가? 다소 혼란 스럽지만, 여기서는 두 가지를 수행 할 코드가 있습니다.
당신은 당신 자신의 Route Handler를 만들 수 있습니다. 당신이 원하는 것을 이제까지 할 수 있습니다. 그런 다음 RouteConfig.cs에서 고유 한 Route Handler를 사용해야합니다.
이것은 MyRouteHander.cs
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace My.Services
{
public class MyRouteHander : IRouteHandler
{
ApplicationDbContext Db = new ApplicationDbContext();
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
// Get route data values
var routeData = requestContext.RouteData;
var action = routeData.GetRequiredString("action");
var controller = routeData.GetRequiredString("controller");
// Get webconfig settings
var webConfigSetting = ConfigurationManager.AppSettings["SOME_FANCY_SETTING"]
if (!string.IsNullOrEmpty(webConfigSetting))
{
requestContext.RouteData.Values["action"] = webConfigSetting;
return new MvcHandler(requestContext);
}
// If we have SomeDataBaseTable hit we do something else.
if (Db.SomeDataBaseTable.Any(x => x.Action == action))
{
// Lets do something with the requestContext.
string actionName = "SpecialAction";
requestContext.RouteData.Values["action"] = actionName;
requestContext.RouteData.Values["controller"] = "SpecialController";
requestContext.RouteData.Values.Add("id", Db.SomeDataBaseTable.FirstOrDefault(x => x.Action == action).Id);
}
return new MvcHandler(requestContext);
}
}
}
App_Start/RouteConfig.cs는 routes.MapRoute()를 업데이트 그래서 당신의 MyRouteHander를 사용합니다.
routes.MapRoute(
"Home",
"Home/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "MyProject.Controllers" }
).RouteHandler = new MyRouteHander();
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "MyProject.Controllers" }
).RouteHandler = new MyRouteHander();
...
희망이 있습니다.
"모든 라우팅"이란 무엇을 의미합니까? 세션 중에 처음으로 페이지 나 웹 사이트에 액세스하는 것에 대해 이야기하고 있습니까? 세션이 시작될 때 무엇인가하고 싶다면'Global.asax'에서'Session_Start' 메소드를 사용해보십시오. –