문제

Ok so a typical route might look like this:

{controller}/{action}/{id}

The problem is that our existing endpoints look like this

{controller}/Somesortofgrouping/{action}

On example of this is our payments controller. We might have the following endpoints:

payment/credit/sale
payment/credit/refund
payment/cash/sale

And the method names would be, respectively:

CreditSale();
CreditRefund();
CashSale();

So rather than having to add every endpoint to the WebApiConfig in order to preserve our convention, is there any way use a template to work with this scenario?

도움이 되었습니까?

해결책

Instead of adding every endpoint to the config, you may consider using the excellent AttributeRouting package and configure routes on the controller/actions itself using attributes.

Some restrictions apply to Web API (as listed in the documentation) as compared to MVC.

After having used this library, it's difficult to imagine going back to maintain a separate configuration of routes.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top