我希望 url 保持与单击提交后的状态相同,我不想显示ActionResult
方法名称
在执行以下代码之前,我的网址是
http://localhost/ProjectName/
[HttpPost]
public ActionResult ControllerSignIn(Models.SignIn signin)
{
ViewBag.name = "John";
return View("~/Views/Home/Index.cshtml");
}
执行上述代码后,我的URL变为
http://localhost/ProjectName/ControllerSignIn/
我也尝试了以下代码
[HttpPost]
public ActionResult ControllerSignIn(Models.SignIn signin,string returnUrl)
{
ViewBag.name = "John";
return View(returnUrl);
}
我的部分查看代码
@using (Html.BeginForm("ControllerSignIn", "Home"))
{
//.... some text box
@Html.Hidden("returnUrl", this.Request.RawUrl)
<input type="submit" class="btn btn-sm btn-primary btn-rounded" value="Login" id="btnLoginSubmit" />
}
注意
我的意思是,无论用户登录哪里,登录后都必须访问相同的网址
答案 0 :(得分:0)
您需要意识到URL决定了应该执行哪个控制器和动作。您传递给View()的不是URL,而是路径。此路径确定应显示哪个视图...
// no matter what you put in "SomePath", your URL will remain the same.
return View("SomePath");
如果要将URL更改为http://localhost/ProjectName/,则需要重定向到该Controller的操作:
[HttpPost]
public ActionResult ControllerSignIn(Models.SignIn signin)
{
ViewBag.name = "John";
/* return View("~/Views/Home/Index.cshtml"); <-- this has no effect on URL */
return RedirectToAction("MyController", "MyAction"); // this would take you to a different URL
}
如果您想重定向到:http://localhost/ProjectName/(我假设ProjectName是您的控制器,并且您想重定向到默认操作)...您需要返回:
return RedirectToAction("ProjectName"); // redirect to default action of ProjectName controller