相关疑难解决方法(0)

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万
查看次数

Api控制器声明多个Get语句

在MVC4中使用新的Api控制器,我发现了一个问题.如果我有以下方法:

public IEnumberable<string> GetAll()

public IEnumberable<string> GetSpecific(int i)

这会奏效.但是,如果我想要检索不同类型的某些不同数据,则默认为该GetAll方法,即使将$.getJSON其设置为GetAllIntegers方法:

public IEnumberable<int> GetAllIntergers()

(错误的命名约定)

我有可能做到这一点吗?

我可以GetAll在Web API控制器中只有一个方法吗?

我认为可视化我想要实现的目标更容易.以下是一段代码,用于展示我希望能够做到的单一内容ApiController:

public IEnumerable<string> GetClients()
{ // Get data
}

public IEnumerable<string> GetClient(int id)
{ // Get data
}

public IEnumerable<string> GetStaffMember(int id)
{ // Get data
}

public IEnumerable<string> GetStaffMembers()
{ // Get data
}
Run Code Online (Sandbox Code Playgroud)

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

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

在Asp.net Mvc 4和Web Api中路由

我可以一起使用以下两条路线规则吗?

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

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)

控制器说是= FruitApiController:ApiController并且我希望有以下内容

  1. List<Fruit> Get() = api/FruitApi/

  2. List<Fruit> GetSeasonalFruits() = api/FruitApi/GetSeasonalFruit

  3. Fruit GetFruits(string id) = api/FruitApi/15

  4. Fruit GetFruitsByName(string name) = api/FruitApi/GetFruitsByName/apple

请帮帮我.谢谢

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

37
推荐指数
1
解决办法
3万
查看次数

如何正确映射WebAPI路由

我正在使用Web API为类似Twitter的网站构建API,并且无法映射路由

我对User控制器执行以下操作:

public User Get(string firstname, string lastname)
public User Get(Guid id)
public User Friends(Guid id)
public User Followers(Guid id)
public User Favorites(Guid id)
Run Code Online (Sandbox Code Playgroud)

所需的路线和生成的文档应该是:

api/users?firstname={firstname}&lastname={lastname}
api/users/{id}
api/users/{id}/friends
api/users/{id}/followers
api/users/{id}/favorites
Run Code Online (Sandbox Code Playgroud)

在WebApiConfig.cs中我有:

config.Routes.MapHttpRoute(
    "2",
    "api/{controller}/{id}",
    new { action = "get", id = RouteParameter.Optional }
);


config.Routes.MapHttpRoute(
     "1",
     "api/{controller}/{id}/{action}"
);
Run Code Online (Sandbox Code Playgroud)

如何正确映射WebAPI路由?

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

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