C#Mvc 3,自定义布线类?
如何在处理路由之前从路由中获取数据 像这样,我得到了以下url:C#Mvc 3,自定义布线类?,c#,asp.net-mvc-3,routing,unity-container,C#,Asp.net Mvc 3,Routing,Unity Container,如何在处理路由之前从路由中获取数据 像这样,我得到了以下url: localhost:1111/{eventid}/controller/method/id {eventid}几乎可以用于整个应用程序中的所有调用。现在,我需要在IOC unity解析器获取控制器之前获取{eventid}。在控制器中,我得到了一个在unity中配置的依赖项,如下所示: container.RegisterType<UserIdent>(new InjectionFactory(c => {
localhost:1111/{eventid}/controller/method/id
{eventid}
几乎可以用于整个应用程序中的所有调用。现在,我需要在IOC unity解析器获取控制器之前获取{eventid}
。在控制器中,我得到了一个在unity中配置的依赖项,如下所示:
container.RegisterType<UserIdent>(new InjectionFactory(c =>
{
...
return GetUser(User.Identity.Name, Session["EventId"].ToString());
}));
container.RegisterType(新注入工厂(c=>
{
...
返回GetUser(User.Identity.Name,会话[“EventId”].ToString());
}));
因此,我需要在unity解决此问题之前设置会话[“EventId”]
因此,基本上我只需要查看EventId是否存在于路由值中(在调用依赖项解析程序之前),以及是否存在,并使用id号创建/更新新的/会话
2) 有没有更好的方法将该id传递给unity,而不是使用具有事件id的会话
这将发生在用户发出的每个请求上。您可以尝试编写正则表达式,它将解析Url并在cookie或会话中设置eventId。 您可以在BeginRequest或HttpModule中实现此功能
至于我,我会编写自己的工厂,它将创建用户,而不是使用unity作为解决方案。这听起来是使用动作过滤器的好例子
public class PopulateEventIdInSessionAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
object eventId = filterContext.ActionParameters["eventid"];
if (eventId != null)
{
Session["EventId"] = (int)eventId;
}
base.OnActionExecuting(filterContext);
}
}
工作得很有魅力。有没有比使用会话将eventid传递给unity更好的方法?Unity会根据上面所示的请求创建一个新的副本,我可以用任何其他不涉及会话的方式传递事件id吗?我已经考虑过了,我想不出更好的方式了。如果有可能更改设计,这样就不需要在实例化时传入,那么这可能值得考虑?