// 控制器类....... public class pligramController:ApiController { // // 获取:/ pligram / public HttpResponseMessage获取( string name) { var reg1 = pligramdata.getalldata(name); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK,reg1); return 响应; } // 模型类。 ... public static 列表< pligramdatamodel> getalldata1( string username){ var query1 =( from u in db.users join hf db.hajj_form中的code-keyword>等于hf.tid join m in db.mehrams on hf.application_no等于m.app_no join fd in db.flighttimes on m.app_no equals fd.app_no join s in db fd.app_no上的.application_status等于s.app_id 其中 u.tid == username // paramt用户............ 选择 new pligramdatamodel { user_name_m = u.user_name, pass_word_m = u.pass_word, name_m = hf.name, cnic_m = hf.cnic, pasport_no_m = hf.pasport_no, groupid_m = hf.groupid, mehram1_m = m.mehram1, app_status_m = s.app_status, goingtime_m = fd.goingtime, returntime_m = fd.returntime, airportlocation_m = fd.airportlocation,}); return query1.ToList(); } 解决方案 最简单的方法不是更改路线,而是更改动作的参数: public 静态列表< pligramdatamodel> getalldata1( string id){ ... where u.tid = = id // 参数用户............ ... } 路由参数会将网址中的值映射到您在网址中建立的变量:财产。在这种情况下: url: {controller} / {action} / {id} 另一种方法: 处理程序本身使用控制器和操作变量来确定要创建的正确对象以及调用它的方法。一般来说,一切都取决于你。如果你愿意,你可以实现一个新的路由,但你需要在那里添加额外的开关以防止冲突,例如你想要getalldata1(字符串用户名)和getalldata1(int id)的方法。 如果您不想利用默认路线,无论出于何种原因,您可以设置不同的路线,例如: routes.MapRoute( name: ByName, url: {controller} / {action} / byname / {username},默认值: new {controller = Home,action = 索引,用户名= UrlParameter.Optional} ); HI! i have a problem with the webAPI route default routes of webAPI is bellow which return all data against a specific controller namepublic static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }its good for return all data and its working fine against bellow codecontroller class... public class pligramController : ApiController { // // GET: /pligram/ public HttpResponseMessage Get() { var reg1 = pligramdata.getalldata1(); HttpResponseMessage response=Request.CreateResponse(HttpStatusCode.OK, reg1); return response; }//model class.... public static List<pligramdatamodel> getalldata1() { var query1 = (from u in db.users join hf in db.hajj_form on u.tid equals hf.tid join m in db.mehrams on hf.application_no equals m.app_no join fd in db.flighttimes on m.app_no equals fd.app_no join s in db.application_status on fd.app_no equals s.app_id where u.tid == hf.tid select new pligramdatamodel { user_name_m=u.user_name, pass_word_m=u.pass_word, name_m=hf.name, cnic_m=hf.cnic, pasport_no_m=hf.pasport_no, groupid_m=hf.groupid, mehram1_m=m.mehram1, app_status_m=s.app_status, goingtime_m=fd.goingtime, returntime_m=fd.returntime, airportlocation_m=fd.airportlocation, }); return query1.ToList(); }i want to get data against a specific string. what should i make changes in routes //controller class....... public class pligramController : ApiController { // // GET: /pligram/ public HttpResponseMessage Get(string name) { var reg1 = pligramdata.getalldata(name); HttpResponseMessage response=Request.CreateResponse(HttpStatusCode.OK, reg1); return response; }//model class....public static List<pligramdatamodel> getalldata1(string username) { var query1 = (from u in db.users join hf in db.hajj_form on u.tid equals hf.tid join m in db.mehrams on hf.application_no equals m.app_no join fd in db.flighttimes on m.app_no equals fd.app_no join s in db.application_status on fd.app_no equals s.app_id where u.tid == username // paramter user............ select new pligramdatamodel { user_name_m=u.user_name, pass_word_m=u.pass_word, name_m=hf.name, cnic_m=hf.cnic, pasport_no_m=hf.pasport_no, groupid_m=hf.groupid, mehram1_m=m.mehram1, app_status_m=s.app_status, goingtime_m=fd.goingtime, returntime_m=fd.returntime, airportlocation_m=fd.airportlocation, }); return query1.ToList(); } 解决方案 The easiest way to do this is not to change routes, but change your action's parameter:public static List<pligramdatamodel> getalldata1(string id) {...where u.tid == id// paramter user...............}The route parameters will map the values in the url into the variables that you establish in the url: property. In this case:url: "{controller}/{action}/{id}"ANOTHER APPROACH:The controller and action variables are used by the handler itself to determine the correct object to create and which method to call on it. Generally everything after that is up to you. You could implement a new route if you wanted to, but you would need to add extra switches in there to prevent collisions, such as if you wanted methods for both getalldata1(string username) and getalldata1(int id).If you didn't want to leverage the default route, for whatever reason, you could put a different route in, such as:routes.MapRoute( name: "ByName", url: "{controller}/{action}/byname/{username}", defaults: new { controller = "Home", action = "Index", username = UrlParameter.Optional } ); 这篇关于如何根据特定字符串获取webAPI中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-26 18:53