在网页API id参数后,用行动路由 [英] Routing with action after id parameter in Web API

查看:168
本文介绍了在网页API id参数后,用行动路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网页API默认路由是:
/ API /位置/ 123?天= 5

  config.Routes.MapHttpRoute(
    名称:DefaultApi
    routeTemplate:API / {}控制器/(编号),
    默认:新{ID = RouteParameter.Optional}
);

但是,如果我想要的路线是这样的 / API /位置/ 123 /事件?天= 5 同时仍然能够打到 LocationsController 像这样 / API /位置/ 123?=状态MD

控制器:

 公共类LocationsController:ApiController {    //获取API /位置/ 123 /事件?天= 5
    公共IEnumerable的<事件> GetEventsByDays(INT idLocation,诠释天){
        // 做东西
    }    //获取API /位置/ 123?=状态MD
    公共IEnumerable的<地点> GetLocationsByState(字符串状态){
        // 做东西
    }
}

有是真的在这里的两个问题:


  1. 是否有意义有一个 LocationsController 返回事件还是应为一个完全独立的控制器?

  2. 如何将在路线 WebApiConfig 进行设置,以允许这样的路线?


解决方案

你说的是大约两型动物的事情:

路由

路由在ASP.NET MVC和放大器使用;网络API直接URL映射到一个控制器和/或一个动作。这是为了提高可读性特别有用,因为开发者可以专注于设计是人类可读(例如,产品支持和搜索引擎索引)的URL。这是在这里重要的是,有一个路由和控制器之间没有唯一的关系。如果你想将映射相同的控制器/动作可以创建10路。

例如,你的两条航线,可就是这样

  config.Routes.MapHttpRoute(
    名称:DefaultApi
    routeTemplate:API / {}控制器/(编号),
    默认:新{ID = RouteParameter.Optional}
);
config.Routes.MapHttpRoute(
    名称:DefaultApi
    routeTemplate:API / {控制器} / {} idLocation /事件/ {}天,
    默认:新{ID = RouteParameter.Optional}
);

另外请注意, / API /位置/ 123?=状态MD 不是默认的路径模板正确。这是 / API /位置/ 123 。因为你在url中有一个额外的参数,您将执行GetLocationsByState。

控制器

它建议报告有每个控制器的单个责任心并保持它尽可能小。你的业务逻辑应该在别处。


  

杰弗里·巴勒莫(洋葱架构的创建者)说


  
  

如果你不能看到屏幕上的ASP.NET MVC的操作方法,而无需滚动,你有问题。


最后,因为你的一切,你可以做你想做的不重视,如果它是好还是坏。困难并不总是设置一个架构,但维护和遵守自己的规则。

我希望这会帮助你。
我想你是不是路由,以便familliar,所以不要犹豫,阅读的介绍和的动作选择

In web api the default route is: /api/locations/123?days=5

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

But what if I wanted the route to look like this /api/locations/123/events?days=5 while still being able to hit the LocationsController with a route like this /api/locations/123?state=md

Controller:

public class LocationsController : ApiController {

    // GET api/locations/123/events?days=5
    public IEnumerable<Event> GetEventsByDays(int idLocation, int days) {
        // do stuff
    }

    // GET api/locations/123?state=md
    public IEnumerable<Location> GetLocationsByState(string state) {
        // do stuff
    }
}

There is really two questions here:

  1. Does it make sense to have a LocationsController that returns events or should there be a completely separate controller for that?
  2. How would the route in the WebApiConfig be setup to allow for routes like this?

解决方案

You're talking about two differents things :

Routing

Routing is used in ASP.NET MVC & Web Api to map URLs directly to a controller and/or an action. This is especially useful for readability, as the developer can focus on designing URLs that are human readable (for example, product support and search engine indexing). What is important here, is that there is no unique relation between a route and a controller. You can create 10 routes if you want that will map the same controller/action.

For example, your two routes can be like this

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

Also note that /api/locations/123?state=md is not correct for the default route template. It's /api/locations/123. Because you have an extra parameter in the url, you will execute GetLocationsByState.

Controller

It's recommanded to have a single responsiblity per controller and to keep it as small as possible. Your business logic should be somewhere else.

Jeffrey Palermo (creator of Onion Architecture) say

if you can’t see a ASP.NET MVC action method on a screen without having to scroll, you have a problem

At the end, as you everything, you can do what you want without paying attention if it's good or bad. The difficulty is not always to setup an architecture but to maintain and to follow your own rules.

I hope this will help you. I suppose you are not so familliar with routing, so do not hesitate to read intro and action selection.

这篇关于在网页API id参数后,用行动路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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