定義問題
我發現有兩種方法可以在 ASP.NET MVC 應用程式中設定會話持續時間:
- 為檔案
timeout中的<sessionState>欄位中的屬性設定一個值web.config。 - 在檔案
Session.Timeout中的Session_Start()方法中設定屬性Global.asax.cs。
當web.config檔案如下實作時,第一種方法不起作用,如
相關源代碼
LoginController.cs檔案中的相關方法和Login/Index.cshtml檔案中定義的腳本如下:
public class LoginController : PublicController
{
private readonly IUserService _userService;
private readonly IUnitOfWork _uow;
private SessionContext _sessionContext;
public LoginController(IUnitOfWork uow, IUserService userService) : base(uow)
{
_uow = uow;
_userService = userService;
_sessionContext = new SessionContext();
}
[HttpPost]
public ActionResult LoginControl(ELoginDTO login)
{
var result = _userService.GetUserByUserName(login.UserName, login.Password);
if (result != null)
{
AutoMapper.Mapper.DynamicMap(result, _sessionContext);
Session["SessionContext"] = _sessionContext;
return Json("/Profile", JsonRequestBehavior.AllowGet);
}
else
{
return Json("", JsonRequestBehavior.AllowGet);
}
}
}
單擊“登錄”按鈕觸發FUNCTION_LoginControl()腳本:
function FUNCTION_LoginControl()
{
var model = { UserName: $("#inputUserName").val(), Password: $("#inputPassword").val() };
if (model.UserName.trim() != "" && model.Password.trim() != "")
{
$.ajax({
url: "/Login/LoginControl",
type: "POST",
data: model,
success: function (e) {
if (e != "")
{
window.location = e;
}
}
});
}
}
uj5u.com熱心網友回復:
我發現當ASP.NET框架cookieless的<sessionState>欄位中的屬性設定為true時,會在URL中添加一個唯一ID。因此,我意識到當我點擊“登錄”按鈕時,我收到了一個錯誤,因為帖子去了而它應該去。/{unique-id}/Login/LoginController/Login/LoginController
解決這個問題的方法是將Web.config檔案中的cookieless欄位設定為:sessionStatefalse
<!-- HttpPost request if "false" is added to the "cookieless" attribute -->
<!-- HttpPost Request: "/Login/LoginController" -->
<sessionState mode="InProc" cookieless="false" timeout="60" />
<!-- HttpPost request if "true" is added to the "cookieless" attribute -->
<!-- HttpPost Request: "/{unique-id}/Login/LoginController" -->
<sessionState mode="InProc" cookieless="true" timeout="60" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/383891.html
標籤:C# 网站 asp.net-mvc 会议 asp.net-ajax
