本文介绍了从其他控制器返回当前视图(MVC登录控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的视图(Home/Index.cshtml):

I have a simple view (Home/Index.cshtml):

@{
  Layout = null;
}

<!DOCTYPE html>
<html>
<head>
  <title>Test Log On</title>
</head>
<body>
  <div id="logon">
    @Html.Partial("_LogOnPartial")
  </div>
  <div id="main">
    Hello
  </div>
</body>
</html>


它正在渲染局部视图(_LogOnPartial.cshtml):


It is rendering a partial view (_LogOnPartial.cshtml):

@using (Html.BeginForm("LogOn", "Account", FormMethod.Post))
{
  <!-- ...fields omitted for simplicity... -->
  <input type="submit" value="Log On" />
}


该部分视图正在发回到另一个控制器(AccountController.cs)上的操作方法:


That partial view is posting back to an action method on a different controller (AccountController.cs):

public class AccountController : Controller
{
  [HttpPost]
  public ActionResult LogOn()
  {
    // ...login logic omitted for simplicity...
    return Redirect(Request.UrlReferrer.PathAndQuery);
  }
}


您会注意到,它只是返回重定向.我认为必须有更好的方法(例如,返回视图),因为这会丢失所有模型数据(如果登录失败,这对于保留数据很有用).我可以绕过视图的路径,但是我不希望客户端访问该数据(并且使用会话来传递该数据看起来很笨拙)……似乎会使我的网站暴露出一些漏洞. br/>
我在想的是,可能有一种方法可以基于引荐来源网址确定默认视图.因此,如果我将"/Home/Index"或"/Home"或"/"或"/SomeOtherValidRoute"传递给函数,它将返回〜/Views/Home/Index.cshtml"视图.

您是否知道这样的功能,还是我会以错误的方式进行操作?在ASP.NET Web窗体中,这只是一个简单的用户控件,但是我在ASP.NET MVC中难以解决.


You''ll note that it is just returning a redirect. I''m thinking there must be a better way (e.g., return a view), as this loses all the model data (which would be useful to keep if the log on fails). I could pass around the path to the view, but I don''t want the client having access to that data (and using session to pass that around seems clunky)... seems like it might expose my website to some vulnerabilities.

What I''m thinking is that there might be a way to determine the default view based on the referrer URL. So if I pass in "/Home/Index" or "/Home" or "/" or "/SomeOtherValidRoute" to a function, it will return the "~/Views/Home/Index.cshtml" view.

Do you know of a function like that, or am I going about this the wrong way? In ASP.NET Web Forms this would just be a simple user control, but I''m having trouble figuring this out in ASP.NET MVC.

推荐答案



这篇关于从其他控制器返回当前视图(MVC登录控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 22:19