Web API Routing error from RC to RTM with model binding - asp.net-mvc-4

Upgrading an rc to rtm web api project
Default parameter binding for simple type parameters is now [FromUri]: In previous releases of ASP.NET Web API the default parameter binding for simple type parameters used model binding. The default parameter binding for simple type parameters is now [FromUri].
I believe is the change that is causing me greif.
Well now I'm not so sure. StrathWeb seems to make me thing it should just work as is.
Given this endpoint
[HttpGet]
public HttpResponseMessage Method(string a, string b)
{
...
}
I generate a url on the client using
#Url.RouteUrl("route", new { httproute = "", controller = "Controller", version = "1" })">
to get it to generate the url for this route.
routes.MapHttpRoute(
name: "route",
routeTemplate: "api/v{version}/{controller}/Method",
defaults: new
{
action = "Method",
controller = "Controller",
version = "1"
});
It creates the url fine. The urls looks like
.../api/v1/Controller/Method?optional=z
.../api/v1/Controller/Method?a=x&b=y&optional=z
It throws a 404 when requested. If I remove the parameters a and b in the api controller then it enters the method just fine.
What is the correct way to make these bind?

if you need 'a' and 'b' to be optional, then you would need to make them optional parameters:
public HttpResponseMessage Method(string a = null, string b = null)

Related

How to dynamically resolve controller with endpoint routing?

Upgrading to asp.net core 2.2 in my hobby project there is a new routing system I want to migrate to. Previously I implemented a custom IRouter to be able to set the controller for the request dynamically. The incoming request path can be anything. I match the request against a database table containing slugs and it looks up the a matching data container class type for the resolved slug. After that I resolve a controller type that can handle the request and set the RouteData values to the current HttpContext and passing it along to the default implementation for IRouter and everything works ok.
Custom implementaion of IRouter:
public async Task RouteAsync(RouteContext context)
{
var requestPath = context.HttpContext.Request.Path.Value;
var page = _pIndex.GetPage(requestPath);
if (page != null)
{
var controllerType = _controllerResolver.GetController(page.PageType);
if (controllerType != null)
{
var oldRouteData = context.RouteData;
var newRouteData = new RouteData(oldRouteData);
newRouteData.Values["pageType"] = page.PageType;
newRouteData.Values["controller"] = controllerType.Name.Replace("Controller", "");
newRouteData.Values["action"] = "Index";
context.RouteData = newRouteData;
await _defaultRouter.RouteAsync(context);
}
}
}
A controller to handle a specific page type.
public class SomePageController : PageController<PageData>
{
public ActionResult Index(PageData currentPage)
{
return View("Index", currentPage);
}
}
However I got stuck when I'm trying to figure out how I can solve it using the new system. I'm not sure where I'm suppose to extend it for this behavior. I don't want to turn off the endpoint routing feature because I see an opportunity to learn something. I would aso appreciate a code sample if possible.
In ASP.NET 3.0 there is an new dynamic controller routing system. You can implement DynamicRouteValueTransformer.
Documentation is on the way, look at the github issue

ASP. NET Web Api 2 issue - The requested resource does not support HTTP method 'GET'

I am not sure why I am getting a "404 Not Found" on the following GET call to my api (using PostMan)
http://localhost:53840/api/v1/MessageLog/SomeStuff/3
The method in the Controller is as follows
[System.Web.Http.HttpGet]
public string SomeStuff(int s)
{
return "Received input !";
}
The Register method in the WebApiConfig class has the only route as follows :
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
But when I change the code to
[System.Web.Http.HttpGet]
public string SomeStuff()
{
return "Received input !";
}
The call http://localhost:53840/api/v1/LogMessage/SomeStuff works and POSTMAN displays the "Recieved input !" string in the response body.
Is there a specific calling convention for passing in int/string etc. (I tried using a [FromUri] without much success) ? I have another POST method in the controlled which takes a JObject and that seems to be working perfectly fine.
It should be something like:
[System.Web.Http.HttpGet]
public string SomeStuff(int id)
{
return "Received input !";
}
Web API matches the parameter by name. In your route template, it is defined as {id} so the action parameter name must match that.
The reason the second one works is because the id is optional and the action matches the template.

How to get current url in view in asp.net core 1.0

In previous versions of asp.net, we could use
#Request.Url.AbsoluteUri
But it seems it's changed. How can we do that in asp.net core 1.0?
You have to get the host and path separately.
#Context.Request.Host
#Context.Request.Path
You need scheme, host, path and queryString
#string.Format("{0}://{1}{2}{3}", Context.Request.Scheme, Context.Request.Host, Context.Request.Path, Context.Request.QueryString)
or using new C#6 feature "String interpolation"
#($"{Context.Request.Scheme}://{Context.Request.Host}{Context.Request.Path}{Context.Request.QueryString}")
You can use the extension method of Request:
Request.GetDisplayUrl()
This was apparently always possible in .net core 1.0 with Microsoft.AspNetCore.Http.Extensions, which adds extension to HttpRequest to get full URL; GetEncodedUrl.
e.g. from razor view:
#using Microsoft.AspNetCore.Http.Extensions
...
Link to myself
Since 2.0, also have relative path and query GetEncodedPathAndQuery.
Use the AbsoluteUri property of the Uri, with .Net core you have to build the Uri from request like this,
var location = new Uri($"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}");
var url = location.AbsoluteUri;
e.g. if the request url is 'http://www.contoso.com/catalog/shownew.htm?date=today' this will return the same url.
You can consider to use this extension method (from Microsoft.AspNetCore.Http.Extensions namespace:
#Context.Request.GetDisplayUrl()
For some my projects i prefer more flexible solution. There are two extensions methods.
1) First method creates Uri object from incoming request data (with some variants through optional parameters).
2) Second method receives Uri object and returns string in following format (with no trailing slash): Scheme_Host_Port
public static Uri GetUri(this HttpRequest request, bool addPath = true, bool addQuery = true)
{
var uriBuilder = new UriBuilder
{
Scheme = request.Scheme,
Host = request.Host.Host,
Port = request.Host.Port.GetValueOrDefault(80),
Path = addPath ? request.Path.ToString() : default(string),
Query = addQuery ? request.QueryString.ToString() : default(string)
};
return uriBuilder.Uri;
}
public static string HostWithNoSlash(this Uri uri)
{
return uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.UriEscaped);
}
Usage:
//before >> https://localhost:44304/information/about?param1=a&param2=b
Request.GetUri(addQuery: false);
//after >> https://localhost:44304/information/about
//before >> https://localhost:44304/information/about?param1=a&param2=b
new Uri("https://localhost:44304/information/about?param1=a&param2=b").GetHostWithNoSlash();
//after >> https://localhost:44304
There is a clean way to get the current URL from a Razor page or PageModel class. That is:
Url.PageLink()
Please note that I meant, the "ASP.NET Core Razor Pages", not the MVC.
I use this method when I want to print the canonical URL meta tag in the ASP.NET Core razor pages. But there is a catch. It will give you the URL which is supposed to be the right URL for that page. Let me explain.
Say, you have defined a route named "id" for your page and therefore, your URL should look like
http://example.com/product?id=34
The Url.PageLink() will give you exactly that URL as shown above.
Now, if the user adds anything extra on that URL, say,
http://example.com/product?id=34&somethingElse
Then, you will not get that "somethingElse" from this method. And that is why it is exactly good for printing canonical URL meta tag in the HTML page.
The accepted answer helped me, as did the comment for it from #padigan but if you want to include the query-string parameters as was the case for me then try this:
#Context.Request.PathBase#Context.Request.GetEncodedPathAndQuery()
And you will need to add #using Microsoft.AspNetCore.Http.Extensions in the view in order for the GetEncodedPathAndQuery() method to be available.
public string BuildAbsolute(PathString path, QueryString query = default(QueryString), FragmentString fragment = default(FragmentString))
{
var rq = httpContext.Request;
return Microsoft.AspNetCore.Http.Extensions.UriHelper.BuildAbsolute(rq.Scheme, rq.Host, rq.PathBase, path, query, fragment);
}
If you're looking to also get the port number out of the request you'll need to access it through the Request.Host property for AspNet Core.
The Request.Host property is not simply a string but, instead, an object that holds both the host domain and the port number. If the port number is specifically written out in the URL (i.e. "https://example.com:8080/path/to/resource"), then calling Request.Host will give you the host domain and the port number like so: "example.com:8080".
If you only want the value for the host domain or only want the value for the port number then you can access those properties individually (i.e. Request.Host.Host or Request.Host.Port).
var returnUrl = string.IsNullOrEmpty(Context.Request.Path) ? "~/" : $"~{Context.Request.Path.Value}{Context.Request.QueryString}";
You may want to get the URL to use it on the razor side, there is an alternative way to get the home app URL:
Url.Content("~/.....")
Example
In the following example, I wanted to generate a QR code and display it in an img tag src, because of using custom route annotation in the Action, I can't use #URL.Action so as an alternative solution I use ~ like this:
<script>
$("#imgCode").attr("src", "#(Url.Content("~/generateQr/"))"+ code);
</script>
Controller Side
[Route("/generateQr/{code}")]
...
ILSpy show how it was done in Microsoft.Owin.dll.
// Microsoft.Owin.OwinRequest
using System;
/// <summary>
/// Gets the uniform resource identifier (URI) associated with the request.
/// </summary>
/// <returns>The uniform resource identifier (URI) associated with the request.</returns>
public virtual Uri Uri => new Uri(string.Concat(Scheme, Uri.SchemeDelimiter, Host, PathBase, Path, QueryString));
I wonder why they removed this property.

OData Routing with Optional Parameter

I have an OData (v3) Web API 2 project that is a wrapper to another wcf web service. The intended client for this odata connection is SharePoint 2013. I am creating CRUD operations within this wrapper and noticed that when sharepoint is asked to delete something it send a request in this format: /Entity(Identity=XX) instead of it's normal /Entity(XX) that i have working normally. I need to be able to handle that request without breaking the other one. Here is my code:
public IHttpActionResult GetSchool([FromODataUri] int key, ODataQueryOptions<School> queryOptions)
{
// validate the query.
try
{
queryOptions.Validate(_validationSettings);
}
catch (ODataException ex)
{
return BadRequest(ex.Message);
}
SchoolDataSource data = new SchoolDataSource();
var result = data.GetByID(key);
return Ok<School>(result);
//return StatusCode(HttpStatusCode.NotImplemented);
}
This works fine for a request for /Schools(1), but not for /Schools(ID=1). i have tried adding:
[Route("Schools(ID={key}")]
And this makes the /Schools(ID=1) route work, but breaks pretty much everything else (406 Errors). i tried adding the above attribute and
[Route("Schools({key})")]to see if i can get them both working, but it doesn't function correctly either. I am very new to this, and was hoping to at least get some direction. Here is my WebApiConfig:
config.MapHttpAttributeRoutes();
config.EnableQuerySupport();
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
// Web API configuration and services
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<School>("Schools");
builder.DataServiceVersion = new Version("2.0");
config.Routes.MapODataRoute("odata", null, builder.GetEdmModel());
// Web API routes
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Errors i get:
406 if i have the route attribute set. 500 if i dont have the route attribute set. it seems as though my service has no idea how to handle the parameter unless i specify it, but if i do, all calls get 406 errors.
may not be the best approach, but made it work with this class:
public class SharePointRoutingConvention : EntitySetRoutingConvention
{
public override string SelectAction(ODataPath odataPath, HttpControllerContext context,
ILookup<string, HttpActionDescriptor> actionMap)
{
//Gets the entity type
IEdmEntityType entityType = odataPath.EdmType as IEdmEntityType;
//makes sure the format is correct
if (odataPath.PathTemplate == "~/entityset/key")
{
//parses out the path segment (Identity=X)
KeyValuePathSegment segment = odataPath.Segments[1] as KeyValuePathSegment;
//Gets the verb from the request header
string actionName = context.Request.Method.ToString();
// Add keys to route data, so they will bind to action parameters.
KeyValuePathSegment keyValueSegment = odataPath.Segments[1] as KeyValuePathSegment;
//Checks to see if the "Identity=" part is in the url
if (keyValueSegment.Value.Contains("Identity="))
{
//removes the extra text
context.RouteData.Values[ODataRouteConstants.Key] = keyValueSegment.Value.Replace("Identity=", "");
}
else
{
//parses it normally
context.RouteData.Values[ODataRouteConstants.Key] = keyValueSegment.Value;
}
//returns the verb
return actionName;
}
// Not a match.
return null;
}
}
and make the change to the webapiconfig:
var conventions = ODataRoutingConventions.CreateDefault();
//adding the custom odata routing convention
conventions.Insert(0, new SharePointRoutingConvention());
config.Routes.MapODataRoute(
routeName: "odata",
routePrefix: null,//this is so that you can type the base url and get metadata back (http://localhost/)
model: builder.GetEdmModel(),
pathHandler: new DefaultODataPathHandler(),
routingConventions: conventions //this assigns the conventions to the route
);

Wrong value binding for asp.net web API controller action paramters

I have an asp.net web api controller action (RESTful) that accepts 2 string parameters. Such a parameter can be empty. The web api action is consumed from AngularJS codes (client side Javascript) in a asp.net Razor view page.
The problem of that web api action is case 4 (see below) is never hit. In details, case 4 is supposed to run when paramter1 is passed with an empty string, and paramter2 is passed with a non-empty string. However, on running for this case and by using debugger, I find the value of paramter1 is bound to the value of parameter2, and the value of parameter2 becomes null or empty. So there is a wrong data binding with that web api action, and I do not know how to solve it. Please help. Thank you.
The web API controller action looks like:
[HttpGet]
[AllowAnonymous]
public HttpResponseMessage GetProductByParamter1AndParameter2(string paramter1, string paramter2)
{
if (string.IsNullOrWhiteSpace(paramter1) && string.IsNullOrWhiteSpace(paramter2))
{
// case 1: do something 1 ...
}
else if (!string.IsNullOrWhiteSpace(paramter1) && !string.IsNullOrWhiteSpace(paramter2))
{
// case 2: do something 2 ...
}
else
{
if (!string.IsNullOrWhiteSpace(paramter1) && string.IsNullOrWhiteSpace(paramter2))
{
// case 3: do something 3 ...
}
else // when paramter1 is empty and paramter2 is not empty
{
// case 4: do something 4 ... but this is never hit
}
}
And the custom route for that web API controller action looks like:
config.Routes.MapHttpRoute(
name: "ProductApi_GetProductByParamter1AndParameter2",
routeTemplate: "api/ProductApi/GetProductByParamter1AndParameter2/{parameter1}/{parameter2}",
defaults: new
{
controller = "ProductApi",
action = "GetProductByParamter1AndParameter2",
parameter1 = "",
parameter2 = ""
}
);
In the cshtml view page, on the client side AngularJS (Javascript codes) to consume that web API, I am coding things like:
myApp.factory('ListProductFactory', function ($http, $q) {
return {
getProducts: function (par1, par2) {
var url = _baseUrl + '/ProductApi/GetProductByParamter1AndParameter2/' + par1 + '/' + par2;
return $http({
method: 'GET',
url: url
})
}
};
});
On the controller you set the 'routeTemplate' as api/ProductApi/**GetSourceColumns**/{parameter1}/{parameter2}, it should be the name of the action GetProductByParamter1AndParameter2.
It doesn't even make sense that the url defined as something like: /ProductApi/GetProductByParamter1AndParameter2/{parameter1}/{parameter2} is still reaching the route that have been defined.
You probably have read these, but if you haven't, check out these links that explain key features of Web API
Model Validation: http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api
Routing and Action Selection: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
To solve this problem, I use query-string approach instead of segment (/) approach:
var url = _baseUrl + '/ProductApi/GetProductByParamter1AndParameter2?parameter1=' + par1 + '&parameter2=' + par2;
I spent 10 days to figure out the answer for myself. It is painful.