使用GET和POST方法的MVC web api [英] MVC web api with GET and POST Method

查看:87
本文介绍了使用GET和POST方法的MVC web api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何执行具有不同名称的MVC web api方法,但类型是GET和POST。我无法执行。它显示错误。这是我的代码。



webapiconfig.cs



 config。 Routes.MapHttpRoute(
name: DefaultApi
routeTemplate: api / {controller} / {id}
默认值: new {action = get,id = RouteParameter.Optional}
);







员工管理员:



 [HttpGet] 
public string Test1()
{
return 这是一个字符串;
}

[HttpGet]
public string Test2()
{
return 这是另一个字符串;
}
[HttpPost]
public string Test3()
{
return 这是另一个帖子字符串;
}





我想在不同场景中执行所有这些方法。怎么做?

解决方案

添加两个或更多路由到配置。所以你用下面的名字来调用api方法

 config.Routes.MapHttpRoute(
name: ApiByName
routeTemplate: api / {controller} / {action} / {name}
默认值: null
constraints: new {name = @ ^ [az] +

}
);
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: 默认
routeTemplate:< span class =code-string> controller / {action} / {id}
默认值: new {controller = 员工 action = test1,id = RouteParameter.Optional}
);





你没有提到控制器。

[httpget],[httppost]与参数区分开来。

您不应该注册所有路线。只需要默认值。

稍后通过点击不同的链接来管理。


Hi,

how to execute MVC web api method with different name but type is GET and POST. i can't able to execute. it shows error. here is my code.

webapiconfig.cs

config.Routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "api/{controller}/{id}",
               defaults: new { action = "get", id = RouteParameter.Optional }
               );




Employee controller :

[HttpGet]
      public string Test1()
      {
          return "this is a string";
      }

      [HttpGet]
      public string Test2()
      {
          return "this is a another string";
      }
   [HttpPost]
        public string Test3()
        {
            return "this is a another post string";
        }



i want to execute all this method in different scenario. how to do it?

解决方案

Add two or more route to the config. So you call api method by using name like below

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


config.Routes.MapHttpRoute(
               name: "Default",
               routeTemplate: "controller/{action}/{id}",
               defaults: new { controller="Employee" action = "test1", id = RouteParameter.Optional }
               );



You didn't mention controller.
[httpget],[httppost] are get differentiate with parameter.
You are not suppose to register all the route. Only default one is required.
Later application manage by clicking on different links.


这篇关于使用GET和POST方法的MVC web api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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