C# 为什么returnUrl值总是空的?
当我尝试登录时,C# 为什么returnUrl值总是空的?,c#,asp.net-core,model-view-controller,C#,Asp.net Core,Model View Controller,当我尝试登录时,returnUrl总是null 我试图从现有的问题中找到答案,但似乎对我的案例没有任何效果 我的创业: services.AddAuthentication( CookieAuthenticationDefaults.AuthenticationScheme ).AddCookie( options => { options.SlidingExpiration = true; options.ExpireTimeSpan = TimeSpan.FromMin
returnUrl
总是null
我试图从现有的问题中找到答案,但似乎对我的案例没有任何效果
我的创业:
services.AddAuthentication( CookieAuthenticationDefaults.AuthenticationScheme ).AddCookie( options => {
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
options.LoginPath = "/home";
options.AccessDeniedPath = "/Error/401";
options.LogoutPath = new PathString("/home/logout");
});
控制器:
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl)
{
try{
if (valid){
if (!String.IsNullOrEmpty(returnUrl))
return LocalRedirect(returnUrl);
}
}
[HttpPost]
[异名]
公共异步任务登录(LoginView模型,字符串返回URL)
{
试一试{
如果(有效){
如果(!String.IsNullOrEmpty(returnUrl))
返回LocalRedirect(returnUrl);
}
}
网址:http://localhost:8888/home?ReturnUrl=%2FRequest%2FNotes%2F1
登录视图:
<form method="post" asp-action="Login">
@if (TempData["ErrorMessage"] != null)
{
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aira-label="close">
<span aria-hidden="true">×</span>
</button>
@TempData["ErrorMessage"]
</div>
}
else if (ViewBag.SessionTimeout != null)
{
<p class="error">@ViewBag.SessionTimeout</p>
}
<div class="form-group">
<input class="form-control" asp-for="Username" placeholder="Username" data-val="true" data-val-required="Your username is required." />
<span data-valmsg-for="Username" data-valmsg-replace="true" class="field-validation-error"></span>
</div>
<div class="form-group">
<input type="password" class="form-control" asp-for="Password" placeholder="Password" data-val="true" data-val-required="Your password is required." />
<span data-valmsg-for="Password" data-valmsg-replace="true" class="field-validation-error"></span>
</div>
<div class="form-action">
<button type="submit" class="btn btn-danger btn-invert btn-wide">Login</button>
</div>
@*<div class="form-meta">
<p>Forgot your password? <a href="#">Reset it.</a></p>
</div>*@
</form>
@if(TempData[“ErrorMessage”]!=null)
{
&时代;
@TempData[“ErrorMessage”]
}
else if(ViewBag.SessionTimeout!=null)
{
@ViewBag.SessionTimeout
}
登录
@*
忘记密码了
*@
您没有将ReturnUrl
与表单一起提交。我假设您将ReturnUrl
存储在ViewData
字典中的HttpGet
中并传递给视图
在您的视图中,您必须将该ReturnUrl
作为查询字符串传递回控制器。只需在表单上添加以下内容:
<form method="post" asp-action="Login" asp-route-ReturnUrl="@ViewData["ReturnUrl"]">
这将把
ReturnUrl
传递回控制器,并将Post请求作为查询字符串。asp route
Helper允许您向url/请求添加单个查询字符串。返回url似乎具有基于您提供的鸥url的值。它怎么可能为空?当您登录时,需要确保返回url为空作为路由参数传递。您可以共享您的登录表单吗?显示部分登录方法有什么意义?您已将控制器操作结果声明为HTTP Post。您只在URL字符串中包含returnUrl
,而不是作为表单输入。在HTTP Post中,数据作为表单键值对传递,而不是作为查询字符串项。@MosiaThabo感谢您提供有关路线参数的建议。这很有帮助