了解ASP.Net MVC中的重定向 [英] Understanding Redirections in ASP.Net MVC

查看:89
本文介绍了了解ASP.Net MVC中的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重定向在ASP.Net MVC中如何工作?

How do redirections work in ASP.Net MVC?

我已经复制并修改了Scott Henselman博客中的代码位 DotNetOpenId通过OpenID登录.调试时,我发现在执行此代码段时:

I've copied and modified the code bit from Scott Henselman's blog that uses DotNetOpenId to login via OpenID. Debugging, I find that when executing this code segment:

// Stage 2: user submitting Identifier
var openId = Request.Form["openId"];
new OpenIdRelyingParty().CreateRequest(openId).RedirectToProvider();
throw new Exception("Should never get here");

我从不使用throw子句,而是继续进行重定向.怎么运作的?

I never get to the throw clause, but rather continue with the redirection. How does that work?

然后,当我从OpenId提供程序获得响应时,我正在通过以下代码段进行调试:

Then, when I get the response from the OpenId provider, I'm debugging through this code segment:

switch (response.Status)
{
  case AuthenticationStatus.Authenticated:
    FormsAuthentication.RedirectFromLoginPage(response.ClaimedIdentifier, false);
    // am I supposed to reach this line? What should I return here?
    // (The method expects a View to be returned)

我发现对FormsAuthentication.RedirectFromLoginPage()的调用确实返回了,我需要从Action中返回一些信息.我应该在这里返回什么?

And I find that the call to FormsAuthentication.RedirectFromLoginPage() does return and I need to return something from the Action. What should I return here?

推荐答案

在您的第一个示例中,RedirectToProvider()方法调用ASP.NET Response.Redirect()方法.由ASP.NET本身实现的此方法引发异常(特别是ThreadAbortException).这就是为什么您永远都不会进入throw new Exception("Should never get here");行的原因.但是请注意,尽管从未达到过,但throw仍必须出现,因为它允许C#知道对于该方法为可验证且安全的代码,无需任何return语句.

In your first example, the RedirectToProvider() method calls the ASP.NET Response.Redirect() method. This method, as implemented by ASP.NET itself, throws an exception (ThreadAbortException specifically). That's why you never get to throw new Exception("Should never get here"); line. Note however that that although it is never reached, throw must still appear because it allows C# to know that no return statement is necessary for the method to be verifiable and safe code.

FormsAuthentication.RedirectFromLoginPage方法最终还会调用Response.Redirect,这意味着它还会引发异常.但是由于您下面没有throw语句,因此C#要求您返回一些内容.当您使用具有返回类型的方法时,C#要求您在每个可能的退出点处返回或抛出.

The FormsAuthentication.RedirectFromLoginPage method also ultimately calls Response.Redirect, which means it also throws an exception. But because you don't have a throw statement beneath it, C# requires that you return something instead. When you're in a method with a return type, C# demands you return or throw at every possible exit point.

在ASP.NET Web表单中,Response.Redirect是您唯一的选择(嗯,虽然不是,但看起来确实如此).但是ASP.NET MVC提供了RedirectAction,它使您可以发出浏览器重定向,而不会引发异常. DotNetOpenAuth(DotNetOpenId的后继者)提供了一种利用此功能的方法.

In ASP.NET web forms, Response.Redirect was your only option (well, not really but it seemed like it). But ASP.NET MVC offers a RedirectAction that allows you to issue a browser redirect without ever throwing an exception. And DotNetOpenAuth (the successor to DotNetOpenId) offers a way to leverage this feature.

此处所示,您可以通过返回IAuthenticationRequest.RedirectingResponse.AsActionResult()的结果来自您的控制器启动登录而不是调用IAuthenticationRequest.RedirectToProvider()的操作.

As shown here you can redirect to the provider by returning the result of IAuthenticationRequest.RedirectingResponse.AsActionResult() from your controller's action to initiate login instead of calling IAuthenticationRequest.RedirectToProvider().

此外,您还可以通过以下方式将用户登录到您的站点,而无需使用FormsAuthentication.RedirectFromLoginPage:先调用FormsAuthentication.SetAuthCookie,然后调用return Redirect(...)在控制器的操作范围内,将用户发送到目的地.

Also, you can log the user into your site without using FormsAuthentication.RedirectFromLoginPage by calling FormsAuthentication.SetAuthCookie and then return Redirect(...) within your controller's action to send the user onto his destination.

这篇关于了解ASP.Net MVC中的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆