English 中文(简体)
不工作的主计长转手
原标题:Redirect from Controller Initialize not working

如果存在某些届会数据,我对控制者来说是压倒一切的。 如果在核对后用户应当停下来,则储存库必须妥善工作。

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    base.Initialize(requestContext);
    if (Session["CompanyID"] != null)
    {
        repo.CompanyID = (long)Session["CompanyID"];
    }
    else
    {
        RedirectToAction("LogOff", "Account");
    }
}

My code looks like this, but even when the RedirectToAction is invoked the controller still opens the default action and the user is not logged off. Can You recommend on how to handle this problem?

我正在以这种方式利用本届会议的数据,因为这是能够首先了解并且在此可以核实是否有这一具体数据的地方。 它是在用户记录中写的。

这一数据是数据库用户的一部分。 我已成为一个习俗成员和角色提供者。 是否有办法将这一数据添加到“User”成员名称的“User”型号中,以便能够在像用户名称一样的构造中使用这些数据?

最佳回答

Consider using a custom ActionFilter instead.

public class HasCompanyIdAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.HttpContext.Session["CompanyID"] == null)
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new {action = "LogOff", controller = "Account"}));
        }
    }
}

It can then be applied as so:

[HasCompanyId]
public class MyController : Controller 
{
    public ActionResult SomeAction()
    {
        return View();
    }
}

This will apply the attribute for all requests that MyController (or it s subclasses) handles.

问题回答

公正执行你们的解决办法,而不是在基类采取行动。 然后,你就可以在《行动记录》中竭尽全力。 与此类似:

public void OnActionExecuting(ActionExecutingContext filterContext){
  if (filterContext.HttpContext.Session["CompanyID"] != null)
     repo.CompanyID = (long)filterContext.HttpContext.Session["CompanyID"];
  else
     filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new {action = "LogOff", controller = "Account"}));
}

某些法典删除了简便。

我尝试了这两种办法,并发现通过行动过滤器加以执行,由于我提出的依赖性注射要求,更为复杂。 你可以这样做,但认为基类办法不太清楚。 我还希望在基类上设定一些特性,使控制器方法能够使用一些标准物体,以节省重复性代码DEVs在行动中增加。 基础班级办法使这种情况变得容易。

我补充说,我不想说这是对认证/安全的一种良好办法,我只想从在行动执行之前进行某种业务/验证的角度来看待这一点,并且还要建立一些关于控制者情况的预先收集的数据,以遵循国家统一原则。

希望会有所助益。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签