mvc uri指向错误的控制器 [英] mvc uri pointing to the wrong controller

查看:94
本文介绍了mvc uri指向错误的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是mvc的新手

i am very new to mvc

//localhost:51525/api/products/GetPromotionTypes">



我得到的控制器如下:


the controller i have got is as bellow

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();
         }



当我从上面的uri调用它指向控制器GetProduc()但我想要它调用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

推荐答案

您应该添加自定义路线如下所示

You should add custom routes like below
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}
默认值: new {action = 获取}
);
config.Routes.MapHttpRoute(
name: DefaultApi
routeTemplate: api / {controller} / {id}
默认值: new {id = RouteParameter.Optional}
);

}
" } ); 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






我找你正在调用WebApi期待结果?



1.有一个配置文件在您的解决方案中,WEB API请求将首先重定向到此配置文件,并决定应该调用哪个控制器来提供请求。该文件是App_Start \WebApiConfig.cs。



我假设您不会对配置文件进行任何更改并保留默认值?如果您已经离开默认,那么这很好,如果没有,我们需要当前的配置



2.你试图打电话的ulr
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







这看起来你修改了App_Start \ WebApiConfig.cs。如果你没有修改网址应该看起来像




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();
         }

}





默认情况下,APi控制器将返回Get()方法当你打电话给控制器时



By default the APi controller will return the Get() method of the controller when you call like

localhost:51525/api/GetPromotionTypes





希望这可以解决一些问题,这取决于你是否修改了配置文件,我上面的解决方案假设你没有修改





更新2 收到进一步澄清后



1.您可能需要更改WebApi配置文件以反映您呼叫的URL



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

localhost:51525/api/products/GetPromotionTypes





请将配置文件更改为





Please change the configuration file as

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();
       }
   }





这告诉您的服务器您的API网址以localhost开头:51525 / api / products



2.你的UR的下一部分是GetPromotionTypes,这意味着你的WebApi控制器名称应该是GetPromotionTypesController,换句话说你的控制器方法应该是shoud是






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


这篇关于mvc uri指向错误的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆