C# WEBApi MVC Help pages

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



C# WEBApi MVC Help pages



I have a custom route below and action.


[System.Web.Mvc.Route("Sites/id:string/Cache")]
public ResponseMessage<Result> DeleteCache ([FromUri] string id)
{



and when I got the the help page it gives three examples to use this call:


DELETE Sites/id/Cache
DELETE Sites/id
DELETE api/Sites/DeleteCache?id=id



I'd like to keep the first one and remove the others. Is there a built in way to do this?



Here is my WebApiConfig.cs snippit....


config.Routes.MapHttpRoute(
name: "DeleteCache",
routeTemplate: "controller/id/Cache",
defaults: new controller = "Sites", action = "DeleteCache"
);

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





What do you mean by "help page"? Is that a page you wrote or something generated (like from Swagger)?
– gunr2171
Aug 7 at 19:43





have you tried to modify the WebApiConfig.cs file?
– AntoOne
Aug 7 at 19:46





@gunr2171 The Microsoft.AspNet.WebApi.HelpPage package.
– jcaruso
Aug 7 at 19:47


Microsoft.AspNet.WebApi.HelpPage





@AntoOne Yes, you can see my routes above now.
– jcaruso
Aug 7 at 19:48





Are you sure that those other two routes trigger your action? They don't look related at all.
– gunr2171
Aug 7 at 19:49




1 Answer
1



HelpPage will list every valid route for each controller. If you want a route to not apply to a specific controller you have to add contraints to the route to make it not match anymore :


config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "controller/id",
defaults: new id = RouteParameter.Optional ,
constraints: new controller = "((?!Sites).)*"
);



This uses a negative lookahead regex to match every Controllers not named Sites





Thanks for this, it appears to work opposite of what I want. I want DELETE Sites/id/Cache to remain while removing the other two, it did the reverse though it is a lead to track down. If you can fix this I'll mark it as accepted answer. Lets see who fixes it first, you or I. :)
– jcaruso
Aug 10 at 15:20


DELETE Sites/id/Cache





I was able to add constraints: new controller = @"^(?:(?!Sites).)*$" to remove a few.
– jcaruso
Aug 16 at 15:54


constraints: new controller = @"^(?:(?!Sites).)*$"






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard