OData v4 Custom functions without namespace in WebAPI 2.2 - asp.net-web-api

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);

Related

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)

Web API get route values

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

Best way to deal with renaming a controller

Working with ASP.NET MVC3, site is in beta and customer decided to rename one of the controllers.
http://domain.com/foo[/*] -> http://domain.com/bar[/*]
What is the most straightforward way to handle redirecting so I don't break any foo bookmarks?
Keep the old controller around so the old URLs still work.
Or add a rewrite rule. Something like:
domain.com/foo(/[_0-9a-z-]+)
to:
domain.com/bar{R:1}
URL Rewrite in IIS
http://technet.microsoft.com/en-us/library/ee215194(WS.10).aspx
http://www.iis.net/download/URLRewrite
If you are using MVC.NET you probably already have URL Rewrite installed.
Another option would be to register a specific route for the old controller name in the Global.asax.cs.
routes.MapRoute(
"RenamedController", // Route name
"[OldControllerName]/{action}/{id}", // URL with parameters
new { controller = "[NewControllerName]", action = "Index", id = "" } // Parameter defaults
);
Add that before the standard default route, and your new controller should respond to both old and new names.
A 302 redirect would be fine, if you can figure out how to do that in IIS. This screenshot suggests it's not that arduous. Alternately, if you're using Castle Windsor you may want to register an interceptor that uses HttpResponse.Redirect()
REST standard suggests the best way to handle this issue is by returning a 301(Moved permanently request). Stack Overflow Post REST Standard
In .Net I recommend using Controller.RedirectToActionPermanent in your controller. See: ASP.NET, .NET Core
Code Example(should work for both ASP and Core):
public class MyController : ControllerBase
{
public IActionResult MyEndpoint(string routeValues1, string routeValues2)
{
return RedirectToActionPermanent("action", "controller", new { value1 = routeValues1, value2 = routeValues2 });
}
}
using MapRoute doesn't make sense in this case. MapRoute is really meant to provide a custom routing solution throughout the system. Its not really meant to deal with individual Redirects. As far as I'm aware it doesn't actually inform the user they are being redirected. See: Creating Custom Routes (C#)

Building URLs in for MVC apps *and* supporting apps

MVC has great tools for building urls inside an MVC app by using routing. Routing by controller and action lets us avoid hard coding urls in markup.
But we have a bunch of ancillary services that need to create urls for marketing emails.
For instance, our email marketing campaigns may need to build a url for an offer. The MVC app knows how to build the url to the offer detail, but is there a clever way of doing this for the service app? Can we/should we move routing into a separate dll and reference it from one place?
Success stories/horror stories are appreciated.
it sounds like you should be using a catchall url to lookup actions you can manage in a DB such as:
Route like this
routes.MapRoute(
"MailCampaigns",
"/mailcampaigns/{*url}",
new {controller = "MailCampaigns", action = "Incoming" }
);
public ActionResult Incoming(string url)
{
//parse the url and perform actions accordingly
var actionInfo = repository.Query<ActionInfo>().Where(x => x.Url == url).SingleOrDefault();
return Redirect(actionInfo.TargetUrl);
}

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