Routing with action after id parameter in Web API

冷暖自知 提交于 2019-12-03 20:35:40

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.

Badri

You can have a separate controller for events, if you would want to manipulate events independent of location. Please see if this is of help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!