简体   繁体   English

ASP.NET MVC2父控制器未重定向

[英]ASP.NET MVC2 Parent Controller Not Redirecting

I have an ASP.NET MVC2 application that uses a parent controller to setup specific variables that are used around the app. 我有一个ASP.NET MVC2应用程序,该应用程序使用父控制器来设置在应用程序周围使用的特定变量。 I also implement validation to make sure that an ID in the URI exists in the database. 我还实现验证以确保URI中的ID在数据库中存在。 If it does not, I redirect and stop the execution of the script. 如果没有,我将重定向并停止脚本的执行。

My parent controller looks something like this: 我的父控制器看起来像这样:


// Inside class declaration

// Set instance of account object to blank account
protected Account account = new Account();

protected override void Initialize(System.Web.Routing.RequestContext requestContext) {
    // Call parent init method
    base.init(requestContext);

    // Check to make sure account id exists
    if (accountRepos.DoesExistById(requestContext.RouteData.Values["aid"].ToString()) {
        account = accountRepos.GetById(requestContext.RouteData.Values["aid"].ToString());
    } else {
        requestContext.HttpContext.Response.Redirect("url");
        requestContext.HttpContext.Response.End();
    }
}

At first this worked, but now when an incorrect id is entered, it doesn't redirect and throws a NullPointerException when the Account class is used. 最初,这种方法有效,但是现在,当输入错误的ID时,它不会重定向,并且在使用Account类时会引发NullPointerException。 I originally just declared the account variable rather instantiating it, but that also proved to throw exceptions and didn't redirect. 我最初只是声明了account变量,而不是实例化了它,但这也证明会引发异常并且没有重定向。

The reason I try to end the execution of the script is because I want to make sure that it stops even if the redirect doesn't work. 我尝试结束脚本执行的原因是,即使重定向无效,我也要确保脚本停止。 Kinda like calling exit() after header() in PHP :p . Kinda喜欢在PHP:p中在header()之后调用exit()。 If I am doing this wrong, I would appreciate any pointers. 如果我做错了,请多多指教。

I'm just wondering how I can fix this. 我只是想知道如何解决此问题。

Any help is greatly appreciated =D 任何帮助都非常感激= D

I don't think that's the proper way to do what you want. 我认为这不是您想要做的正确方法。 Instead you should use route constraints on your routes to make sure the id exists, and fall back from there in a "catch all" route. 相反,您应该在路线上使用路线约束来确保ID存在,然后从那里退回到“全部捕获”路线。

Something like this: 像这样:

Routes.MapRoute("Name", "Url", new { ... }, new {
    Id = new IdConstraint() // <- the constraint returns true/false which tells the route if it should proceed to the action
});

The constraint would be something like this: 约束将是这样的:

public class IdConstraint : IRouteConstraint {
    public bool Match(
        HttpContextBase Context,
        Route Route,
        string Parameter,
        RouteValueDictionary Dictionary,
        RouteDirection Direction) {
        try {
            int Param = Convert.ToInt32(Dictionary[Parameter]);

            using (DataContext dc = new DataContext() {
                ObjectTrackingEnabled = false
            }) {
                return (dc.Table.Any(
                    t =>
                        (t.Id == Param)));
            };
        } catch (Exception) {
            return (false);
        };
    }
}

This is what I use with my routes to make sure that I'm getting an Id that really exists. 这就是我在路线中使用的功能,以确保我得到的ID确实存在。 If it doesn't exist, the constraint returns a false, and the route does not execute and the request continues down the route chain. 如果不存在,则约束返回false,并且路由不执行,并且请求沿路由链继续。 At the very bottom of your routes you should have a generic catch all route that sends your user to a page that tells them what they want doesn't exist and to do X or X (something along those lines, I'm just coming up with scenarios). 在您的路由的最底部,您应该有一条通用的捕获所有路由,该路由会将您的用户发送到一个页面,该页面告诉他们他们想要的东西不存在,并执行X或X(类似的做法,我刚来与方案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM