c# - How to use session in apicontroller in asp.net mvc 4 -
i working on application using asp.net mvc 4
. here want use autocomplete extender using jquery
want populate location of cityid
stored in session.
here function creating session:
public string create(string city) { try { //httpsessionstatebase session["c"]=city; //httpcontext.current.session["city"] = city; system.web.httpcontext.current.session["city"] = city; long cityid = convert.toint64(system.web.httpcontext.current.session["city"].tostring()); return city; } catch (exception ex) { throw (ex); } }
this function called when user select city city dropdownlist
.
my jquery
calling autocomplete extender is:
<script type="text/javascript"> var url = '@url.routeurl("defaultapi", new { httproute = "", controller = "productapi" })'; $('#txtlocation').autocomplete({ source: function (request, response) { alert(url); $.ajax({ url: url, data: { query: request.term }, datatype: 'json', type: 'get', success: function (data) { response($.map(data, function (item) { return { label: item.name //value: item.id } })); } }) }, select: function (event, ui) { $('#txtlocation').val(ui.item.label); //$('#id').val(ui.item.value); return false; }, minlength: 1 });
my api controller :
public class productapicontroller : apicontroller { sqlconnection cnn = new sqlconnection(system.configuration.configurationmanager.appsettings["sqlconn"].tostring()); [httpget] public ienumerable<search> getproducts(string query = "") { cnn.open(); //string gid = getsession("t"); long cityid = convert.toint64(system.web.httpcontext.current.session["city"].tostring()); sqlcommand cmd = new sqlcommand("check_prefixtext", cnn); cmd.parameters.addwithvalue("city", cityid); cmd.parameters.addwithvalue("@prefix", query); cmd.commandtype = commandtype.storedprocedure; sqldataadapter da = new sqldataadapter(cmd); datatable dt = new datatable(); da.fill(dt); //var result = ienumerable<city>(query); search obj = new search(); cnn.close(); return dt.asenumerable().select(row => { return new search { name = convert.tostring(row["name"]), }; }); } }
in global.asax
file have written 2 methods:
protected void application_postauthorizerequest() { if (iswebapirequest()) { httpcontext.current.setsessionstatebehavior(sessionstatebehavior.required); } } private bool iswebapirequest() { return httpcontext.current.request.apprelativecurrentexecutionfilepath.startswith(webapiconfig.urlprefixrelative); }
in webapiconfig
class have written follows:
public static string urlprefix { { return "api"; } } public static string urlprefixrelative { { return "~/api"; } } public static void register(httpconfiguration config) { config.routes.maphttproute( name: "defaultapi", routetemplate: webapiconfig.urlprefix + "/{controller}/{id}", defaults: new { id = routeparameter.optional } ); }
but still value of session["city"]
coming null in apicontroller
while there there value stored showing in session["city"]
.
change api to:
[httpget] public ienumerable<search> getproducts(long cityid, string query = "") {
then pass cityid data
data: { cityid: $("#citydropdownid :selected").val(), query: request.term },
forget trying use "session" - it's not correct technology trying do.
Comments
Post a Comment