WebApi ApiExplorer ApiExplorerSettingsAttribute and IgnoreApi for Action Parameters - asp.net-web-api

You can decorate a controller or action method with the ApiExplorerSettingsAttribute setting the IgnoreApi property to NOT generate help info. If you try to apply the same to an action method's attribute, you get an error:
public HttpResponseMessage Post([ApiExplorerSettings(IgnoreApi = true)]HttpRequestMessage request, ... )
Error 2 Attribute 'ApiExplorerSettings' is not valid on this declaration type. It is only valid on 'class, method' declarations.
A common convention for keeping your controller actions testable is to accept an HttpRequestMessage parameter, but this is an implementation detail, not something your API consumers should know about.
How can I prevent the ApiExplorer from including this parameter when it generates the help page?

For clarity...by this "if you try to apply the same to an action method's attribute", did you mean you were trying to apply to the parameter?
-A quick fix would be is to add an additional check to the existing CancellationToken check that we have in this file: \Areas\HelpPage\Views\Help\DisplayTemplates\Parameters.cshtml
// Don't show CancellationToken because it's a special parameter
if (!typeof(CancellationToken).IsAssignableFrom(parameter.ParameterDescriptor.ParameterType))
{
-Also, you could avoid having HttpRequestMessage as a parameter on action as you can get the current request from the Request property on the controller, but you wanted it as a parameter for testing...is it?

Related

pagination ruins other actions webapi

I'm trying to implement pagination for a resource in webapi, but it ruined other action which return singleById resource.
So there are two functions for getting object/s in my api ( one single another mass but paged ) like below:
public HttpResponseMessage Get(int page,string type="mm");
public HttpResponseMessage Get(int id);
The type option is a semi search like optional filter.
I know it has conflicts i made it string id but it didn't work, i thought about a way routing each action separately or renaming them but it doesn't work cause the default route is like api/{controller}/{id} and doesn't include action names beside i don't like to have api/{controller}/post/ to be generated.
I decided to use Attribute Routing
[Route("specefiedController/{id}/single")]
to have getById action and let the other actions be as they were

Passing extra parameter through GetAll method of webapi

How to pass an extra parameter through a Get method of webapi because when i pass
GetALL(int page,int limit,int start) it works fine but when in passed one more parameters that is optional and may be null it throws error.
GetAll(int page,int limit,int start,string ? search)
What is the best way to make it working
In Web API optional parameters are those which can be nulled.
If you have type values like int or DateTime, you need to make them nullableby using the ? syntax.
But when they're classes instead of value type, they are directly convertible to null, so you don't need to, and can not, mark them as nullable. SO, your method signature must be simply this:
GetAll(int page,int limit,int start,string search)
If you wanted page, limit or start to be nullable, you should declare them as int?. So, int he signature above this 3 parameters are compulsory, and the last one optional.
EDIT, for OP comment
When you use the default routing for Web API the only way to choose the right method is by parameter matching, i.e. the parameters in the request must match the parameters in the action including the optional parameters. So, there are two ways to make it work:
post the optional parameters as empty parameters. For your case, provided you're using the query string, include a &search= in the URL
modify the routes, so that the parameters are provided as route parameters, and define the search parameter as optional
You can also completely modify the web API routing by including the action in the route. In that case, you have to specify the action in the URLs to invoke the action, but the method can be chosen by action name (given by method name or Action attribute), and not by parameter matching. In that case you don't need to provide the optional parameters. It will work like MVC routing.

How to call the WebApi,Implemented with AttributeRouting for each action methods,calling it from my client and passing the Query parameters in url?

I have implemented attribute routing for each of action methods in My Webapi.
Example of action method is:-
[Route("api/DocumentApi/DeleteDocument/{fileInfoId}/{customerAccountName}")]
[HttpDelete]
public HttpResponseMessage DeleteDocument(int fileInfoId, string customerAccountName)
{
//***
//*** Some body contents
//***
}
Now i want to call the above action method from the client example( Fiddler Web debugger),or browser and want to pass the Url request in the below pattern:-
http://{localhost:9791}/api/DocumentApi/DeleteDocument?fileInfoId=12&customerAccountName="Manish"
Currently i am not able to hit the above action method by the above specified url request.
But if i use the url pattern like below:-
http://{localhost:9791}/api/DocumentApi/DeleteDocument/12/Manish
I am able to hit the above action method.But for my project requirement,I need to use the Url with query parameters only.
Please suggest me the approach,how to achieve this?
Any response will be greatly appreciated.
Route templates in Web API do not support specifying query string parameters. For your scenario, do not define fileInfoId and customerAccountName as part of your route template as doing so makes Web API to strictly look for 5 segments(the text between the / characters in your route template) in your request url...so just modify your route template to [Route("api/DocumentApi/DeleteDocument")] and keep the parameters on the actions as it is...
Use like following code :
[Route("api/DocumentApi/DeleteDocument/{fileInfoId}/{customerAccountName}")]
public HttpResponseMessage TestAction(string fileInfoId,string customerAccountName)
{

Change Action to be executed on ASP.NET WebAPI with ActionFilterAttribute

My goal is to develop a Custom FilterAttribute, there i can apply to an action on ASP.NET Web API, where one of two thing should happens, but the resul must be the same:
Expected result: if some condition is verified, the action execution should be cancelled or redirected and a different object should be returned;
on a filterattribute to mvc i just have to change the actionname when im overriding a specific method.
does any one have ideas how to do the same on a webapi filterattribute?
SOLUTION:
if (true)
{
IHttpRouteData x = request.Properties["MS_HttpRouteData"] as IHttpRouteData;
x.Values["action"] = "IsUnavailableBecause";
}
I'm not sure I understand what you are trying to do. Can you give an example?
Using message handlers would allow you to change the action route parameter before action selection has occurred.

need the query string from the request to use with the input attribute of the action mapping

I am using struts 1.3.10. I need the query string from the request to use with the input attribute of the action mapping so that when validation fails, the forward goes to correct page without any null pointer exceptions. how can I do this? I do have the entire forward(myAction.do?foo="bar") as a form property posted throught he jsp. Plesae let me know if I am not clear on the problem definition.
Have you tried request.getQueryString()?
Example:
String queryString = request.getQueryString(); //where "request" is "HttpServletRequest"

Resources