的代码片段会向您提供无法编译。您必须从所有代码路径返回结果。尝试是这样的:
public ActionResult Visit()
{
int visit = 0;
if (base.IsLoggedIn() == true)
{
visit++;
return PartialView();
}
else
{
return RedirectToAction("ACTION_NAME_GOES_HERE");
}
}
UPDATE:
我相信我现在明白你要完成的任务。如果ajax请求由未登录的用户发出,您希望浏览器重定向用户。我建议修改你的操作方法如下:
public ActionResult Visit()
{
int visit = 0;
if (base.IsLoggedIn())
{
visit++;
// whatever else needs to be done
return PartialView();
}
else
{
// Note that this could be done using the Authorize ActionFilter, using
// an overriden Authorize ActionFilter, or by overriding OnAuthorize().
if (!base.IsLoggedIn())
return new HttpUnauthorizedResult();
}
}
假设你在你的web.config配置了,响应将是302重定向这是不是你在这种情况下想要的东西。见菲尔哈克的博客文章,说明此项ASP.NET行为如何防止和401未授权的响应可以返回:
http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx
然后,假设你使用jQuery发出AJAX请求,您可以处理401响应:
$.ajax({
url: '/your_controller/visit',
type: 'GET',
statusCode: {
200: function (data) {
// data = your partialview
},
401: function (data) {
location.href = '/the_path/to_redirect/to';
}
}
});
并非所有的代码路径都返回结果。用户登录时应该发生什么? – Pieter
不明白正在问什么。 – RayLoveless