Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am very new to mvc
HTML
//localhost:51525/api/products/GetPromotionTypes">

the controller i have got is as bellow
C#
public IEnumerable<Product> GetAll()
        {
            return Utility.GetDiscountItems();
        }

        public Product GetProduct(string Id)
        {
            return Utility.GetProduct(Id);
        }
        public String PostBag(Bag bagofItem)
        {
            return Utility.PostBagDiscountedItem(bagofItem);
        }
        public List<PromotionType> GetPromotionTypes()
         {
             return Utility.GetPromotionTypes();
         }

when i call from the above uri it pointing to the controller GetProduc() but what i wanted it to call GetPromotionTypes()

what i have done wrong
appreciate all your help
Posted
Comments
Jameel VM 22-Aug-13 7:27am    
please post the controller class also
Jameel VM 22-Aug-13 7:28am    
i think you are implementing the mvc controller class.for web api you should implement the api controller

You should add custom routes like below
C#
public static void Register(HttpConfiguration config)
        {
             config.Routes.MapHttpRoute(
              name: "ApiByName",
              routeTemplate: "api/{controller}/{action}/{name}",
              defaults: null,
              constraints: new { name = @"^[a-z]+$" }
             );
            config.Routes.MapHttpRoute(
                name: "ApiByAction",
                routeTemplate: "api/{controller}/{action}",
                defaults: new { action = "Get" }
            );
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
           
        }


Hope this helps
 
Share this answer
 
v2
Comments
rushdy20 22-Aug-13 6:43am    
sorry by replace the above not did not work. now it even not hetting any of the controllers
have i missed any think out
Hi,

It looks for me you are calling a WebApi expecting the results ?

1. There is a configuration file within your solution and WEB API request will be redirected to this config file first and decided which controller should be called to serve the request. The file is App_Start\WebApiConfig.cs.

I presume you would not have done any changes to the config file and left with default? If you have left with default then that is fine and if not, we need the current config

2. the ulr you are trying to call
localhost:51525/api/products/GetPromotionTypes



This looks that you have modified the App_Start\WebApiConfig.cs. If you have not modified the url should looks something like
localhost:51525/api/GetPromotionTypes
and this should have a controller and the method

public class GetPromotionTypes : ApiController

{

public IEnemerable<promotiontype> Get()
         {
             return Utility.GetPromotionTypes();
         }

}


By default the APi controller will return the Get() method of the controller when you call like
localhost:51525/api/GetPromotionTypes


Hope this sheds some light and all depends on whether you have modifed the config file or not and my solution above is assuming that you have not modified


UPDATE 2 After further clarification received

1.You may have to change you WebApi Configuration file to reflect the URL you are calling
C#
localhost:51525/api/products/GetPromotionTypes


Please change the configuration file as

C#
public static class WebApiConfig
   {
       public static void Register(HttpConfiguration config)
       {
           config.Routes.MapHttpRoute(
               name: "ProductApi",
               routeTemplate: "api/products/{controller}/{id}",
               defaults: new { id = RouteParameter.Optional }
           );



           // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
           // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
           // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
           //config.EnableQuerySupport();
       }
   }


This tells your server that your API url starts with localhost:51525/api/products

2. Next part of your UR is GetPromotionTypes, this means your WebApi Controller name should be "GetPromotionTypesController" in other words your controller method shoud be


public class GetPromotionTypesController : ApiController
   {
      public IEnemerable<promotiontype> Get()
         {
             return Utility.GetPromotionTypes();
         }


   }


Hope this resoles your problem, if still any issues let me know
 
Share this answer
 
v7
Comments
rushdy20 22-Aug-13 8:23am    
no i have not changed it is as bellow
public class RouteConfig
{
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 }
);
}
}
Bala Selvanayagam 22-Aug-13 8:31am    
You are looking at the wrong configuration file

There are two configuration file one for WEB API routing and one for MVC routing. the one you were looking at is MVC routing


look at the correct WEB API routing configuration which is under App_Start/WebApiConfig.cs
rushdy20 22-Aug-13 8:47am    
sorry yes the webapiConfig as bellow
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Bala Selvanayagam 22-Aug-13 9:21am    
Please see my comments under "UPDATE 2" in the solution section above

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