简体   繁体   English

配置路由asp.net webapi

[英]Configuring Routes asp.net webapi

I am building an angularJS application with a asp.net webapi backend. 我正在使用asp.net webapi后端构建angularJS应用程序。 In my routeconfig file, i have this 在我的routeconfig文件中,我有这个

routes.MapRoute(
    name: "default",
    url: "{*url}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

This works fine. 这很好。 Any Url that is called is returned the Home/Index view (the only view i have) to the application, and angularJS works out if there is a querystring and works out which state to show. 任何被调用的Url都将Home / Index视图(我拥有的唯一视图)返回给应用程序,而angularJS会在存在查询字符串的情况下得出结论并指出要显示的状态。

I have the basic Get, Put, Post and Delete methods in my WebApi, and i can call them fine. 我的WebApi中具有基本的Get,Put,Post和Delete方法,我可以称呼它们为好。 Examples are 例子是

public class CompanyController : ApiController
{
    private CompanyService _service;

    public CompanyController(CompanyService service)
    {
        _service = service;
    }

    public async Task<IHttpActionResult> Get()
    {
        ...

        return Ok(model);
    }

    public async Task<IHttpActionResult> Get(int id)
    {
        ...

        return Ok(model);
    }

    public async Task<IHttpActionResult> Post(CompanyModel model)
    {
        ...

        return Ok();
    }

    public async Task<IHttpActionResult> Put(Company model)
    {
        ...

        return Ok();
    }

    public async Task<IHttpActionResult> Delete(CompanyModel model)
    {
        ...

        return Ok();
    }
}

Now i would like to add another method to my api, where the user can load companies, but also pass in a term to search for (a string), a pageSize (int) and a page number (int). 现在,我想在我的api中添加另一种方法,用户可以在其中加载公司,还可以传递一个术语来搜索(字符串),pageSize(int)和页码(int)。 Something like this 像这样

public async Task<IHttpActionResult> Get(string term, int page, int pageSize) {
    ...

    return Ok(results);
}

Now i understand that i need to add another route, to make sure this method can be called. 现在我知道我需要添加另一条路由,以确保可以调用此方法。 Fine, so i add this to my RouteConfig. 很好,所以我将其添加到RouteConfig中。

// search
routes.MapRoute(
    name: "search",
    url: "api/{controller}/{page}/{pageSize}/{term}",
    defaults: new { page = @"\d+", pageSize = @"\d+", term = UrlParameter.Optional }
);

Why doesnt this work?? 为什么不起作用? I got a resource cannot be found error, when trying to call it via postman using the url localhost/api/company/1/10/a, where 1 = page, 10 = pageSize and a = term 尝试通过邮递员使用url / localhost / api / company / 1/10 / a通过邮递员调用它时,我得到了一个资源找不到错误,其中1 =页面,10 = pageSize和a =术语

Its probably a simple answer, but new to MVC so still learning. 这可能是一个简单的答案,但是对于MVC来说还很陌生,因此仍然值得学习。

1- You are using Get method, which means you can pass your search option via Url, so you can create a search option object like : 1-您正在使用Get方法,这意味着您可以通过Url传递搜索选项,因此您可以创建一个搜索选项对象,例如:

public class SearchOptions
{
   public string Term{get; set;}
   public int Page {get; set;}
   public int PageSize {get; set;}
}

then you can change your method to be like this 然后您可以将方法更改为这样

[HttpGet]
[Route("api/blabla/SearchSomething")]
public async Task<IHttpActionResult> Get([FromUri]SearchOptions searchOptions) {
    ...

    return Ok(results);
}

Notice the Route attribute that I've decorated the method by, you can use different constraints for the method parameters, have a look at this . 注意,我装饰了该方法的Route属性,您可以对方法参数使用不同的约束,请看一下this

Finally you can call the method from the client like this 最后,您可以像这样从客户端调用方法

api/blabla/SearchSomething?term=somevalue&page=1&pageSize=10

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM