Web API get route values - asp.net-web-api

Is there any way to statically get route values from a service method (outside of a controller) that is running in a Web API context? For example, I can do the following in ASP.NET MVC:
var mvcHandler = HttpContext.Current.Handler as MvcHandler;
var routeValues = mvcHandler.RequestContext.RouteData.Values;
I'd like to find the equivalent version of this code for Web API.
When I try to debug a sample Web API request and look at HttpContext.Current.Handler it is of type HttpControllerHandler, but this type doesn't have any properties to access route data.
EDIT
To try to help provide some more information. The code I am trying to read the value from is inside of a factory class I have that builds a custom object for my application.

You can use GetRouteData() extension on HttpRequestMessage. You would need to include System.Net.Http namespace to get this.
System.Web.Http.Routing.IHttpRouteData routeData = Request.GetRouteData();

I was able to find a solution that would get the route values for either an MVC request or a Web API request.
HttpContext.Current.Request.RequestContext.RouteData

Related

Dynamic Web API Controllers and WithConventionalVerbs()

I'm using Dynamic Web API Controllers but my Get methods are being created as POST.
evidence
I've already set ".WithConventionalVerbs()" but no success
Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
.ForAll<IApplicationService>(typeof(AssisteVidaApplicationModule).Assembly, "app")
.WithConventionalVerbs()
.Build();
My Put and Delete methods are okay.
What's wrong?
The only thing you could miss; there could be [HttpPost] attribute on PedidoAppService.Get() and PedidoAppService.GetAll() methods. Can you check if there's any HttpPost attribute on these methods? And to understand the problem you can rename the Get method to GetSomething() ... if http verb gets correct we can have more info about the problem.

Return Pdf Docs from Controller in ASP.NET Core MVC

I tried to get Pdf Docs from my IActionResult method using APIs like RazorPdf,MvcPdfActionResult,PerfectPdf... But none is working for me. I am Developing an Application in ASP.NET Core MVC.
Are there any supported APIs for generating Pdfs from Controller Actions....?
MvcPdfActionResult worked for me with... I just had to change the ViewName.
Instead of passing it directly, I tried giving it's relative path like ~/Views/PdfView.cshtml.
The actual code looks like this...
return new PdfActionResult("~/Views/PdfView.cshtml", modelobject)

OData v4 Custom functions without namespace in WebAPI 2.2

We want to use ODataV4 on WebAPI 2.2 for our new projects but there is one no-go: the url design.
We have to delivery json data and binary data (images) through our api.
Is there any possibility to avoid the namespace look of custom functions on OData routes?
/odata/Customers/GetByName('Name') instead of /odata/Customers/CustomerService.GetByName('Name')
And how should a response with binary data (jpeg images) be implemented with OData controllers?
A very ugly way would be to host OdataControllers and ApiControllers in one Project and use different url areas.
/odata/Customers(1) -> OdataControllers
/api/Customers/1/ProfileImage -> ApiController
For your first question, please refer to http://odata.github.io/WebApi/#06-01-custom-url-parsing.
Thank you, QianLi! You really helped!
HttpConfiguration config = …
config.EnableCaseInsensitive(caseInsensitive: true);
config.EnableUnqualifiedNameCall(unqualifiedNameCall: true);
config.EnableEnumPrefixFree(enumPrefixFree: true);
config.MapODataServiceRoute("odata", "odata", edmModel);

Parameter format issue with ASP.NET Web API controller

I am watching this pluralsight demo video on Web Api, and he is using fiddler to pass in a parameter using Http Get with the syntax of controller/parameter
So he's using http://localhost:2405/api/values/5
5 Is the parameter he's passing in.
In my code, I have everything set up exactly the same way he does... with a routing template of {controller}/{id} and a controller method with a signature of
public string Get(string zipcode)
I can pass a parameter just fine with http://localhost:2405/api/values?zipcode=25252 but if I try passing in a paramter the way he does, like http://localhost:2405/api/values/25252 I get an error saying I do not have an action available to handle that request on the controller.
What is he doing right, that I'm doing wrong?
You need to change your routing template to {controller}/{zipcode} as the name of the parameter must match the name in the template.

Google ajax api

Is it possible to use Google AJAX API :
on local google search engine for example : google.es
or is it only working for google.com?
Thanks for helping.
It is possible, and there are a few ways to do it.
See this entry on Googles AJAX API blog.
There are three possible ways to implement, depending on how you're using the API:
If you use the loader, you can simply load jsapi on the domain you're
interested in (example), such as:
Alternately, you can set this with the web search object's
.setRestriction method (example):
var ws = new google.search.WebSearch();
ws.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS,
{'gl' : 'es'});
Finally, if you're using the RESTful interface, all you have to do
is append a "gl" URL parameter to your
request:
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=flowers&gl=fr

Resources