English 中文(简体)
检查每个页面请求的条件
原标题:Check conditions on each page request

我有多租户ASP.NET MVC应用程序,它利用子域来确定当前租户。域是否有效是通过数据库表查找来确定的。

哪里是检查域是否在数据库中的函数的最佳位置?如果子域不在数据库中,则应重定向到Error控制器中的Index操作。

将检查放在Global.asax文件中的Application_BeginRequest方法中不起作用,因为会产生永不终止的重定向。

最佳回答

哪里是检查域是否在数据库中的函数的最佳位置?如果子域不在数据库中,则应重定向到“错误”控制器中的“索引”操作。

将检查放在Global.asax文件中的Application_BeginRequest方法中是不起作用的,因为会产生永无止境的重定向。

这是正确的位置,您只需要检查请求Url是否已经存在/Error。

您可能已经在这样做了,但我想补充一点,它似乎是非常静态的信息,您应该缓存这些信息,而不是针对每个请求访问数据库。

问题回答

u可以子类化actionFilter属性并覆盖onactionExecuting方法。在这种方法中,u可以进行任何数据库检查并适当地重定向用户

public class CustomActionFilterAttribute : ActionFilterAttribute
{
     public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if(DatabaseLookup)
        {
           return;
        }
        filterContext.Result = new RedirectResult("http://servername/Error");

    }

}

现在,您可以使用这个自定义actionfilter属性装饰您的操作方法

[CustomActionFilter]
public ActionResult mymethod()
{
    //action method goes here
}




相关问题
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 (...

热门标签