gpt4 book ai didi

c# - 哪个是重定向 URL 的最佳方式?我们应该使用 Response.Redirect 还是有其他重定向命令?

转载 作者:太空宇宙 更新时间:2023-11-03 17:39:09 24 4
gpt4 key购买 nike

我是 Csharp ASP.NET 和 MVC 的新手。我正在尝试自定义登录页面并试图保持 的条件检查登录是否经过身份验证。 如果登录完成,那么下次用户输入 url: localhost:portno/account/login网址将被定向到 localhost:portno/home/index .我使用了 Respond.Redirect 方法

@model LearningMvc.Models.LoginViewModel 

完整代码如下:
    @model LearningMvc.Models.LoginViewModel

@{
ViewBag.Title = "Log in";
}

<h2>@ViewBag.Title.</h2>
<div class="row">
<div class="col-md-8">
<section id="loginForm">
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
if (Request.IsAuthenticated)
{
Response.Redirect("~/home/index");
}
else {
<h4>Use a local account to log in.</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.UserName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Password)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
<p>
@Html.ActionLink("Register", "Register") if you don't have a local account.
</p>
}
}
</section>
</div>
<div class="col-md-4">
<section id="socialLoginForm">
@Html.Partial("_ExternalLoginsListPartial", new { Action = "ExternalLogin", ReturnUrl = ViewBag.ReturnUrl })
</section>
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

我担心的只是功能
if (Request.IsAuthenticated)
{
Response.Redirect("~/home/index");
}
else {}

当我在 Web 浏览器中运行代码时,我在 Visual Studio 中遇到了一个异常,如下所述:
    System.Web.HttpException was unhandled by user code
HResult=-2147467259
Message=Server cannot append header after HTTP headers have been sent.
Source=System.Web
ErrorCode=-2147467259
WebEventCode=0
StackTrace:
at System.Web.HttpResponse.AppendHeader(String name, String value)
at System.Web.HttpResponse.AddHeader(String name, String value)
at System.Web.HttpResponseWrapper.AddHeader(String name, String value)
at System.Web.Helpers.AntiXsrf.AntiForgeryWorker.GetFormInputElement(HttpContextBase httpContext)
at System.Web.Helpers.AntiForgery.GetHtml()
at System.Web.Mvc.HtmlHelper.AntiForgeryToken()
at ASP._Page_Views_Shared__LoginPartial_cshtml.Execute() in c:\Users\Sanjog Sigdel\Documents\Visual Studio 2013\Projects\LearningMvc\LearningMvc\Views\Shared\_LoginPartial.cshtml:line 6
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName)
at ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\Users\Sanjog Sigdel\Documents\Visual Studio 2013\Projects\LearningMvc\LearningMvc\Views\Shared\_Layout.cshtml:line 28
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer)
at System.Web.WebPages.WebPageBase.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer)
at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
at System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content)
at System.Web.WebPages.WebPageBase.Write(HelperResult result)
at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
at System.Web.WebPages.WebPageBase.PopContext()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
InnerException:

我在 Visual Studio 中按继续后,代码就可以工作了。
是什么造成了这个异常?

最佳答案

您不应该在您的 View 中执行任何非演示逻辑!这不应该出现在 View 中:

if (Request.IsAuthenticated)
{
Response.Redirect("~/home/index");
}

存在问题是因为 View 正在执行并且它已经发送了有关响应类型的 header (在本例中为 text/html)。

相反,您的逻辑应该在 Controller 中:
public ActionResult MyAction()
{
/// ... code

if (Request.IsAuthenticated)
{
return RedirectToAction("index", "home");
}

return View();
}

关于c# - 哪个是重定向 URL 的最佳方式?我们应该使用 Response.Redirect 还是有其他重定向命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38082514/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com