Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 用于标识ASP.NETMVC应用程序中当前会话信息的泛型类_Asp.net Mvc_Session_Model View Controller_Base Class - Fatal编程技术网

Asp.net mvc 用于标识ASP.NETMVC应用程序中当前会话信息的泛型类

Asp.net mvc 用于标识ASP.NETMVC应用程序中当前会话信息的泛型类,asp.net-mvc,session,model-view-controller,base-class,Asp.net Mvc,Session,Model View Controller,Base Class,我正在使用ASP.Net MVC开发一个简单的自定义基于角色的Web应用程序,在我的登录操作中,我正在创建一个配置文件会话,如下所示: [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(LoginModel model, string returnUrl) { using (HostingEnvironment.Impersonate()) { if (

我正在使用ASP.Net MVC开发一个简单的自定义基于角色的Web应用程序,在我的登录操作中,我正在创建一个配置文件会话,如下所示:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    using (HostingEnvironment.Impersonate())
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                var employeeProfile = AccountBal.Instance.GetEmployee(loginId);
                Session["Profile"] = employeeProfile;
                FormsAuthentication.SetAuthCookie(model.UserName, true);
            }
        }
        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", @"The user name or password provided is incorrect.");
        return View(model);
    }
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateOrEdit(MyModel model)
{
    var employee = (Employee) Session["Profile"];
    if (employee == null) 
       return RedirectToAction("Login", "Account");

    if (ModelState.IsValid)
    {
        // Functionality goes here....
    }
}   
我在所有控制器操作中检查或使用此会话,如下所示:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    using (HostingEnvironment.Impersonate())
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                var employeeProfile = AccountBal.Instance.GetEmployee(loginId);
                Session["Profile"] = employeeProfile;
                FormsAuthentication.SetAuthCookie(model.UserName, true);
            }
        }
        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", @"The user name or password provided is incorrect.");
        return View(model);
    }
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateOrEdit(MyModel model)
{
    var employee = (Employee) Session["Profile"];
    if (employee == null) 
       return RedirectToAction("Login", "Account");

    if (ModelState.IsValid)
    {
        // Functionality goes here....
    }
}   
是否有任何方法可以将这段会话检查代码移动到基类或集中类中?因此,我不需要每次在控制器操作中都检查它,而是直接访问属性


创建包含GetCurrentProfile方法的基本控制器,以检索当前用户配置文件,如

public class BaseController : Controller
{
    public Employee GetCurrentProfile()
    {
        return (Employee)Session["Profile"];
    }

    public bool SetCurrentProfile(Employee emp)
    {
        Session["Profile"] = emp;
        return true;
    }
}
并使用上述BaseController继承所需的控制器,并访问GetCurrentProfile方法,如下所示

public class HomeController : BaseController
{
    public ActionResult SetProfile()
    {
        var emp = new Employee { ID = 1, Name = "Abc", Mobile = "123" };

        //Set your profile here
        if (SetCurrentProfile(emp))
        {
            //Do code after set employee profile
        }
        return View();
    }

    public ActionResult GetProfile()
    {
        //Get your profile here
        var employee = GetCurrentProfile();

        return View();
    }
}
GetCurrentProfile和SetCurrentProfile可直接用于所需的控制器,因为我们直接从BaseController继承它

您可以在上面的代码段中使用try/catch


尝试一次可能会帮助您

使用BaseController继承控制器,并在该控制器中写入方法GetCurrentProfile,并在其中添加会话代码GetCurrentProfile,并使用此方法,如var employee=GetCurrentProfile;以你想要的方式controller@55SK55,查看答案可能对您有帮助: