Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI! i have a problem with the webAPI route default routes of webAPI is bellow which return all data against a specific controller name

C#
public 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 code

controller class...

C#
 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





C#
//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();
       
       }
Posted

1 solution

The easiest way to do this is not to change routes, but change your action's parameter:

C#
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:

C#
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:

C#
routes.MapRoute(
              name: "ByName",
              url: "{controller}/{action}/byname/{username}",
              defaults: new { controller = "Home", action = "Index", username = UrlParameter.Optional }
          );
 
Share this answer
 
Comments
Member 11504333 19-Nov-15 11:51am    
thnks..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900