0

SPA MVC4の新機能であり、アカウントコントローラーからLinqToEntitiesDataControllerにセッション変数を渡して、次の方法でユーザーを識別しようとします。

                      using (DALEntities db = new DALEntities())
                        {
                            string PHN = (from p in db.Patients
                                          where p.UserName == model.UserName
                                          select p.PHN).First();
                            Session["P"] = PHN;
                        }

LinqToEntitiesDataControllerで、次のように変数を使用しようとしています。

public partial class DALController : LinqToEntitiesDataController<MyVDC.Models.DALEntities>
    {
        public IQueryable<MyVDC.Models.TestModel> GetTestModel()
        {
            **string phn = (string)Session["P"];**
            return ObjectContext.TestModels.Where(b => b.PHN == phn).OrderBy(b => b.ID);

        }
}

このエラーが発生します:

The name 'Session' does not exist in the current context

これが唯一の方法ですか、それともこのコントローラーでセッション変数を使用するためのより良い方法がありますか。

アカウントコントローラーでも使用しようとしました。

 HttpContext.Current.Session["P"] = PHN;

しかし、私はこのエラーを受け取ります:

'System.Web.HttpContextBase' does not contain a definition for 'Current' and no extension method 'Current' accepting a first argument of type 'System.Web.HttpContextBase' could be found 

前もって感謝します。

4

1 に答える 1

0
public class SessionHttpControllerRouteHandler : HttpControllerRouteHandler
{
    protected override IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
    {
        return new SessionHttpControllerHandler(requestContext.RouteData);
    }
}

public class SessionHttpControllerHandler : HttpControllerHandler, IRequiresSessionState
{
    public SessionHttpControllerHandler(RouteData routeData) : base(routeData) { }
}

セッションサポートHttpControllerHandlerを作成し、Global.asaxでこのクラスのエントリをルーティングします。

    routes.Add("SessionApis",
        new HttpWebRoute(
            url: "api/{controller}/{id}",
            defaults: new RouteValueDictionary(new {id = RouteParameter.Optional}),
            routeHandler: new SessionHttpControllerRouteHandler()
    ));

しかし、ApiControllerはセッションレスで使用するのに適していると思います。とてもスケーラブルでRESTの原則。

LinqToEntitiesDataControllerルートは、カスタムAreaRegistrationクラス(DALRouteRegistrationクラス?)で追加します。足場のデフォルトエリアレジスタコードを変更します。

から

    RouteTable.Routes.MapHttpRoute(
        "DAL", // Route name
        "api/DAL/{action}", // URL with parameters
        new { controller = "DAL" } // Parameter defaults
    );

    RouteTable.Routes.Add("SessionDALApis",
        new HttpWebRoute(
            url: "api/DAL/{action}",
            defaults: new RouteValueDictionary(new { controller = "DAL" }),
            routeHandler: new SessionHttpControllerRouteHandler()
    ));

これはどう?

于 2012-04-28T15:14:17.540 に答える