标签: asp.net-web-api-routing

找到了与Web Api中的请求匹配的多个操作

当我尝试使用2个"Get"方法时,我不断收到此错误

找到了与请求匹配的多个操作:webapi

我一直在寻找关于堆栈的其他类似问题,但我没有得到它.

我有2个不同的名称,并使用"HttpGet"属性

[HttpGet]
public HttpResponseMessage Summary(MyVm vm)
{
    return null;
}

[HttpGet]
public HttpResponseMessage FullDetails()
{
    return null;
}
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-web-api asp.net-web-api-routing

229
推荐指数
7
解决办法
18万
查看次数

Post参数始终为null

自从升级到RC for WebAPI后,我在WebAPI上调用POST时遇到了一些奇怪的问题.我甚至回到了新项目生成的基本版本.所以:

public void Post(string value)
{
}
Run Code Online (Sandbox Code Playgroud)

并从Fiddler打来电话:

Header:
User-Agent: Fiddler
Host: localhost:60725
Content-Type: application/json
Content-Length: 29

Body:
{
    "value": "test"
}
Run Code Online (Sandbox Code Playgroud)

当我调试时,字符串"value"永远不会被分配给.它总是为NULL.谁有这个问题?

(我第一次看到更复杂类型的问题)

问题不仅仅是绑定到ASP.NET MVC 4,在安装RC后,新的ASP.NET MVC 3项目也会出现同样的问题

.net c# asp.net-web-api asp.net-web-api-routing

195
推荐指数
11
解决办法
31万
查看次数

Web API控制器中的多个HttpPost方法

我开始使用MVC4 Web API项目,我有多种HttpPost方法的控制器.控制器如下所示:

调节器

public class VTRoutingController : ApiController
{
    [HttpPost]
    public MyResult Route(MyRequestTemplate routingRequestTemplate)
    {
        return null;
    }

    [HttpPost]
    public MyResult TSPRoute(MyRequestTemplate routingRequestTemplate)
    {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

这里MyRequestTemplate表示负责处理通过请求的Json的模板类.

错误:

当我让使用招为请求http://localhost:52370/api/VTRouting/TSPRoutehttp://localhost:52370/api/VTRouting/Route 我得到一个错误:

找到了与请求匹配的多个操作

如果我删除上述方法之一,它工作正常.

Global.asax中

我已经尝试修改默认路由表global.asax,但我仍然收到错误,我认为我在global.asax中定义路由时遇到问题.这是我在global.asax中所做的.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapHttpRoute(
        name: "MyTSPRoute",
        routeTemplate: "api/VTRouting/TSPRoute",
        defaults: new { }
    );

    routes.MapHttpRoute(
        name: "MyRoute",
        routeTemplate: "api/VTRouting/Route",
        defaults: new { action="Route" }
    );
}
Run Code Online (Sandbox Code Playgroud)

我正在使用POST在Fiddler中发出请求,在RequestBody中为MyRequestTemplate传递json.

c# global-asax asp.net-web-api asp.net-web-api-routing

126
推荐指数
5
解决办法
17万
查看次数

任何人都可以向我解释CreatedAtRoute()吗?

从Web API 2的模板,post方法总是这样:

[ResponseType(typeof(MyDTO))]
public IHttpActionResult PostmyObject(MyDTO myObject)
{
    ...
    return CreatedAtRoute("DefaultApi", new { id = myObject.Id }, myObject);
}
Run Code Online (Sandbox Code Playgroud)

我不明白这种CreatedAtRoute()方法.任何人都可以CreatedAtRoute()向我解释这个方法吗?

c# asp.net asp.net-web-api asp.net-web-api-routing

120
推荐指数
3
解决办法
4万
查看次数

ASP.NET Web API中的自定义方法名称

我正在从WCF Web API转换为新的ASP.NET MVC 4 Web API.我有一个UsersController,我想要一个名为Authenticate的方法.我看到了如何进行GetAll,GetOne,Post和Delete的示例,但是如果我想在这些服务中添加额外的方法呢?例如,我的UsersService应该有一个名为Authenticate的方法,它会传入用户名和密码,但是它不起作用.

public class UsersController : BaseApiController
{
    public string GetAll()
    {
        return "getall!";
    }

    public string Get(int id)
    {
        return "get 1! " + id;
    }

    public User GetAuthenticate(string userName, string password, string applicationName)
    {
        LogWriter.Write(String.Format("Received authenticate request for username {0} and password {1} and application {2}",
            userName, password, applicationName));

        //check if valid leapfrog login.
        var decodedUsername = userName.Replace("%40", "@");
        var encodedPassword = password.Length > 0 ? Utility.HashString(password) : String.Empty;
        var leapFrogUsers = LeapFrogUserData.FindAll(decodedUsername, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api asp.net-web-api-routing

109
推荐指数
6
解决办法
15万
查看次数

Web Api属性路由中的可选参数

我想处理以下API调用的POST:

/v1/location/deviceid/appid

附加参数来自Post-Body.

这对我来说都很好.现在我想通过允许"deviceid"和/或"appid"和/或BodyData为null来扩展我的代码:

/v1/location/deviceid
/v1/location/appid
/v1/location/
Run Code Online (Sandbox Code Playgroud)

这3个URL应该由相同的路由响应.

我的第一种方法(需要BodyData):

[Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")]
public location_fromuser Post(string deviceid = null, string appid = null, [FromBody] location_fromuser BodyData)
{
    return repository.AddNewLocation(deviceid, appid, BodyData);
}
Run Code Online (Sandbox Code Playgroud)

这不起作用并返回编译错误:

"可选参数必须在最后"

接下来尝试:

[Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")]
public location_fromuser Post([FromBody] location_fromuser BodyData, string deviceid = null, string appid = null)
Run Code Online (Sandbox Code Playgroud)

现在我的函数AddNewLocation()总是得到BodyData=null- 即使调用发送Body.

最后我设置了所有3参数可选:

[Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")]
public location_fromuser Post(string deviceid = null, string appid = null, [FromBody location_fromuser BodyData = null)
Run Code Online (Sandbox Code Playgroud)

不工作:

BodyData不支持可选参数FormatterParameterBinding …

c# asp.net asp.net-web-api asp.net-web-api-routing

81
推荐指数
4
解决办法
11万
查看次数

.NET WebAPI属性路由和继承

我正在使用一个基本控制器的想法,该控制器使用通用存储库为我的API控制器提供基本的CRUD方法,这样我就不必在每个新控制器中复制相同的基本代码.但是,当它在基本控制器中时,会遇到路由属性被识别的问题.为了准确显示我遇到的问题,我创建了一个非常简单的WebAPI控制器.

当我在主控制器中有Get方法并且它直接从ApiController继承时,我没有任何问题,这可以按预期工作.

[RoutePrefix("admin/test")]
public class TestController : ApiController
{
    [Route("{id:int:min(1)}")]
    public string Get(int id)
    {
        return "Success";
    }
}
Run Code Online (Sandbox Code Playgroud)

当我将Get方法移动到基本控制器时,它返回404页面的内容.

[RoutePrefix("admin/test")]
public class TestController : TestBaseController
{

}

public class TestBaseController : ApiController
{
    [Route("{id:int:min(1)}")]
    public string Get(int id)
    {
        return "Success";
    }
}
Run Code Online (Sandbox Code Playgroud)

一些更有趣的笔记:

  • 我可以在GET/Test/1上访问该操作.所以它仍然基于默认路线找到它.

  • 当我尝试访问POST/admin/test时,它返回以下JSON

    {"Message":"找不到与请求URI匹配的HTTP资源' http://test.com/admin/test'.","MessageDetail ":"找不到与名为'admin'的控制器匹配的类型. " }

有没有人知道如何让路由使用基本控制器的属性?

asp.net-web-api asp.net-web-api-routing

64
推荐指数
2
解决办法
3万
查看次数

在ASP.NET Web API中使用多个Get方法进行路由

我正在使用Web Api和ASP.NET MVC,我对它很新.我已经在asp.net网站上进行了一些演示,我正在尝试执行以下操作.

我有4个get方法,具有以下签名

public List<Customer> Get()
{
    // gets all customer
}

public List<Customer> GetCustomerByCurrentMonth()
{
    // gets some customer on some logic
}

public Customer GetCustomerById(string id)
{
    // gets a single customer using id
}

public Customer GetCustomerByUsername(string username)
{
    // gets a single customer using username
}
Run Code Online (Sandbox Code Playgroud)

对于上面的所有方法,我希望我的web api有点像下面所示

  • 列表Get()= api/customers/
  • Customer GetCustomerById(string Id)= api/customers/13
  • 列出GetCustomerByCurrentMonth()= /customers/currentMonth
  • Customer GetCustomerByUsername(string username)= /customers/customerByUsername/yasser

我尝试对路由进行更改,但由于我是新手,因此无法理解.

所以,请一些人帮助我理解并指导我如何做到这一点.谢谢

c# asp.net-web-api asp.net-web-api-routing

60
推荐指数
5
解决办法
13万
查看次数

使用属性路由时查询字符串不起作用

我正在使用System.Web.Http.RouteAttributeSystem.Web.Http.RoutePrefixAttribute为我的Web API 2应用程序启用更干净的URL.对于我的大部分请求,我可以使用路由(例如Controller/param1/param2)或者我可以使用查询字符串(例如Controller?param1=bob&param2=mary).

不幸的是,对于我的一个控制器(并且只有一个),这会失败.这是我的控制器:

[RoutePrefix("1/Names")]
public class NamesController : ApiController
{

    [HttpGet]
    [Route("{name}/{sport}/{drink}")]
    public List<int> Get(string name, string sport, string drink)
    {
        // Code removed...
    }

    [HttpGet]
    [Route("{name}/{drink}")]
    public List<int> Get(string name, string drink)
    {
        // Code removed...
    }
}
Run Code Online (Sandbox Code Playgroud)

当我使用路由请求时,两者都可以正常工作.但是,如果我使用查询字符串,它会失败,告诉我该路径不存在.

我已经尝试将以下内容添加到我的WebApiConfig.cs类' Register(HttpConfiguration config)函数(在默认路由之前和之后),但它没有做任何事情:

config.Routes.MapHttpRoute(
name: "NameRoute",
routeTemplate: "{verId}/Names/{name}/{sport}/{drink}",
defaults: new { name = RouteParameter.Optional, sport = RouteParameter.Optional, drink = RouteParameter.Optional },
constraints: new { verId = @"\d+" }); …
Run Code Online (Sandbox Code Playgroud)

c# query-string asp.net-web-api attributerouting asp.net-web-api-routing

60
推荐指数
6
解决办法
7万
查看次数

找不到与名为"User"的控制器匹配的类型

我正在尝试导航到一个页面,其URL的格式如下:localhost:xxxxx/User/{id}/VerifyEmail?secretKey = xxxxxxxxxxxxxxx

我在RouteConfig.cs文件中添加了一个新路由,所以我RouteConfig.cs看起来像这样:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "VerifyEmail",
            url: "User/{id}/VerifyEmail",
            defaults: new { controller = "User", action = "VerifyEmail" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index",
                id = UrlParameter.Optional }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我尝试导航到该URL时,我得到了这个页面:

<Error>
    <Message>
        No HTTP resource was found that matches the request URI 'http://localhost:52684/User/f2acc4d0-2e03-4d72-99b6-9b9b85bd661a/VerifyEmail?secretKey=e9bf3924-681c-4afc-a8b0-3fd58eba93fe'.
    </Message>
    <MessageDetail>
        No type was found that matches the controller …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-routing asp.net-mvc-4 asp.net-web-api asp.net-web-api-routing

47
推荐指数
6
解决办法
7万
查看次数