I am unable to hit Get method in WebAPI with umbraco project - asp.net-web-api

I have trouble to hit webapi action.
I am getting 500 error in the console and the api is not reached.
This is my ajax call:
function getProducts() {
var response;
$.getJSON("/api/Product")
.done(function (data) {
response = $.map(data,
function (item) {
return { label: item.Name + ' (' + item.Code + ')' };
});
});
return response;
};
The ajax is not hitting. The request skips getJson call and returns undefined response.
This is my api controller method:
public class ProductController : ApiController {
// GET api/<controller>
public IEnumerable<ProductModel> ProductList()
{
ProductSearcher searcher = new ProductSearcher();
return searcher.GetResults();
}
}
In the config I have defined:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Postman returns Object reference not set to an instance of an object but it doesn't even hit the ProductList either.
I have selected to run multiple projects - my app start project and web api project.
This is the error stacktrace - it looks to me like umbraco does looks for routing while I need to route to non-umbraco api:
[NullReferenceException: Object reference not set to an instance of an object.]
Umbraco.Web.Routing.ContentLastChanceFinderByNotFoundHandlers.HandlePageNotFound(PublishedContentRequest docRequest) +152
Umbraco.Web.Routing.ContentLastChanceFinderByNotFoundHandlers.TryFindContent(PublishedContentRequest docRequest) +10
Umbraco.Web.Routing.PublishedContentRequestEngine.HandlePublishedContent() +529
Umbraco.Web.Routing.PublishedContentRequestEngine.FindPublishedContentAndTemplate() +250
Umbraco.Web.Routing.PublishedContentRequestEngine.PrepareRequest() +107
Umbraco.Web.UmbracoModule.ProcessRequest(HttpContextBase httpContext) +361
Umbraco.Web.UmbracoModule.<Init>b__8(Object sender, EventArgs e) +80
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +71
EDIT
I changed the routing config from api to dataapi eg. routeTemplate: "dataapi/{controller}/{id}" so it now runs but still throws an error:
405 Method not allowed
EDIT 2
Got it! Changed the method name from ProductList to Get and it's now working

You could use an UmbracoApiController instead, to make sure that Umbraco doesn't try to match the URLs to content? Then the path to the controller would be /umbraco/api/yourcontroller instead, but everything should work the same. Take a look at https://our.umbraco.com/documentation/reference/routing/webapi/ for details.

Related

ASP.NET Core API response headers not in expected place

I have an ASP.NET Core API that adds two headers to its response callback_uri and redirect_uri.
The strange thing (to me) is that in my AJAX call to the service, the headers are part of the JSON data, as a headers array, rather than the request object itself. I cannot use jqxhr.getResponseHeader(...) and therefore must interrogate the headers array manually within the response data.
Because the StatusCode is also part of the data it means my AJAX success callback is always called, even when I'm testing for a 400 bad request response, which makes testing less simple.
Web API controller action:
[HttpGet, Route("Authenticate")]
public HttpResponseMessage Authenticate(string applicationId)
{
HttpResponseMessage response;
if(!_security.IsApplicationIdValid(applicationId))
{
response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
response.ReasonPhrase = ErrorMessages.INVALID_APPLICATION_ID;
return response;
}
IAuthenticationProvider authProvider = _security.GetAuthenticationProvider();
response = new HttpResponseMessage(System.Net.HttpStatusCode.Redirect);
response.Headers.Add(HeaderKeyNames.CALLBACK_URI_KEY_NAME, authProvider.GetCallbackUrl());
response.Headers.Add(HeaderKeyNames.AUTHENTICATION_SERVICE_REDIRECT_URI_KEY_NAME, authProvider.GetUrl());
return response;
}
AJAX code:
var settings = {
data: { "applicationId": applicationId },
success: successCallback, // at the moment just writes to console
error: errorCallback, // at the moment just writes to console
method: "GET"
};
$.ajax(url, settings);
Am I doing something wrong on the server-side?
You can use a combination of ResultFilters and ServiceFilterAttribute to add your custom headers. This is particularly useful because:
ServiceFilter enables you to have DI access in your ResultFilter.
You can apply it as an Attribute in the actions you want
You can test it.
Putting all together:
Create the custom result filter class
public class CustomHeadersResultFilter : IResultFilter
{
private readonly IMyService _myService;
public CustomHeadersResultFilter(IMyService myService)
{
_myService = myService;
}
public void OnResultExecuting(ResultExecutingContext context)
{
context.HttpContext.Response.Headers.Add("my-header", _myService.GetData());
// if under CORS, this need to be added otherwise you can't read the headers using xhr.getResponseHeader('my-header')
context.HttpContext.Response.Headers.Add("Access-Control-Expose-Headers", "my-header");
}
public void OnResultExecuted(ResultExecutedContext context)
{
// can't add headers here, since it's too late in the pipeline
}
}
Register it in your Startup.ConfigureServices
services.AddTransient<IMyService, MyService>();
// our custom result filter
services.AddTransient<CustomHeadersResultFilter>();
Apply the attribute in the action you want to return the custom headers
[HttpGet("{id}")]
[ServiceFilter(typeof(CustomHeadersResultFilter))]
public ActionResult Get(string id)
{
if (id == "something-bad")
{
return BadRequest("invalid application id");
}
// return a 200 Ok. Check the other types if you want something different
return Ok();
}
Testing all of this with a separate web application, doing an ajax request to the API, you can access the headers:
<script>
var settings = { method: "GET" };
$.ajax('http://localhost:61284/api/values/test', settings)
.done(function (data, textStatus, xhr) {
alert(xhr.getResponseHeader('my-header'));
})
.fail(function () {
alert("error");
});
</script>
Add headers like this: (ofc change the type if needed or define your own)
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
What you're doing is creating a HttpResponseMessage object, serializing it to json and then returning it.
This is why the headers are in the json content, instead of the http response.
What you can do is someting like this:
[HttpGet, Route("Authenticate")]
public IActionResult Authenticate(string applicationId)
{
if(!_security.IsApplicationIdValid(applicationId))
{
return BadRequest(ErrorMessages.INVALID_APPLICATION_ID);
}
IAuthenticationProvider authProvider = _security.GetAuthenticationProvider();
this.Response.Headers.Add(HeaderKeyNames.CALLBACK_URI_KEY_NAME, authProvider.GetCallbackUrl());
this.Response.Headers.Add(HeaderKeyNames.AUTHENTICATION_SERVICE_REDIRECT_URI_KEY_NAME, authProvider.GetUrl());
return StatusCode(302);
}

Route action based on HTTP verb?

I'm trying to get ASP.NET Core 2 MVC to route the action based on the HTTP verb via the following code in Startup.cs:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "post",
template: "api/{controller}/{id?}",
defaults: new { action = "Post" },
constraints: new RouteValueDictionary(new { httpMethod = new HttpMethodRouteConstraint("POST") })
);
routes.MapRoute(
name: "delete",
template: "api/{controller}/{id?}",
defaults: new { action = "Delete" },
constraints: new RouteValueDictionary(new { httpMethod = new HttpMethodRouteConstraint("DELETE") })
);
routes.MapRoute(
name: "default",
template: "api/{controller}/{action=Get}/{id?}");
});
I.e.,
If the client calls GET http://example.com/api/foo, that runs the Get() method on my FooController : Controller class.
If they call GET http://example.com/api/foo/123, that runs the Get(int id) method on my FooController : Controller class.
If they call POST http://example.com/api/foo, that runs the Post([FromBody] T postedItem) method on my FooController<T> : Controller class.
If they call POST http://example.com/api/foo/123, that runs the Post(int id, [FromBody] T postedItem) method on my FooController<T> : Controller class.
If they call DELETE http://example.com/api/foo/123, that runs the Delete(int id) method on my FooController : Controller
When I run the project, it doesn't seem to run any of my controllers. I have some Razor pages that respond but all of the controller-based routes just return 404. Not even the default route seems to work.
I've been using https://github.com/ardalis/AspNetCoreRouteDebugger to try and help me narrow the issue down but I'm still not finding the problem. It shows the methods on the controllers as available actions but doesn't list any of the names, templates or constraints added via MapRoute. I'd be glad to know of any other helpful tools as well.
FWIW, I'm trying to use the same verb constraints as here:
https://github.com/aspnet/Routing/blob/2.0.1/src/Microsoft.AspNetCore.Routing/RequestDelegateRouteBuilderExtensions.cs#L252-L268
So I don't recall exactly what the problem turned out to be but the meta-solution is that you can debug routing problems by increasing the log level from "Information" to "Debug". E.g., via appsettings.json:
{
"Logging": {
"Debug": {
"LogLevel": {
"Default": "Debug"
}
},
"Console": {
"LogLevel": {
"Default": "Debug"
}
}
}
}
...then you'll get messages like this in e.g., the Application Output pane of Visual Studio:
[40m[37mdbug[39m[22m[49m: Microsoft.AspNetCore.Routing.RouteConstraintMatcher[1]
Route value '(null)' with key 'httpMethod' did not match the constraint 'Microsoft.AspNetCore.Routing.Constraints.HttpMethodRouteConstraint'.
Microsoft.AspNetCore.Routing.RouteConstraintMatcher:Debug: Route value '(null)' with key 'httpMethod' did not match the constraint 'Microsoft.AspNetCore.Routing.Constraints.HttpMethodRouteConstraint'.
[40m[37mdbug[39m[22m[49m: Microsoft.AspNetCore.Routing.RouteBase[1]
Request successfully matched the route with name 'get' and template 'api/{controller}/{id?}'.
Microsoft.AspNetCore.Routing.RouteBase:Debug: Request successfully matched the route with name 'get' and template 'api/{controller}/{id?}'.
[40m[37mdbug[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Executing action Contoso.Media.ServiceHost.Controllers.MediaController.Get (Contoso.Media.ServiceHost)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Debug: Executing action Contoso.Media.ServiceHost.Controllers.MediaController.Get (Contoso.Media.ServiceHost)

WebAPI : 403 Forbidden after publish website

Alright, I'm having a tough time locating the problem since it works locally but after doing a publish the results are simply:
Error Code: 403 Forbidden. The server denied the specified Uniform Resource Locator (URL). Contact the server administrator. (12202)
The code:
[RoutePrefix("api/v1/project")]
public class ProjectController : BaseApiController
{
[HttpGet]
public HttpResponseMessage GetProjects()
{
HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK);
if(User.Identity.IsAuthenticated)
{
var model = new ModelFactory().CreateProjects();
resp = Request.CreateResponse(HttpStatusCode.OK, model);
}
return resp;
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// all actions under /project routes require authentication
config.Routes.MapHttpRoute(
name: "ProjectApi",
routeTemplate: "api/v1/{controller}/{action}/{apikey}",
defaults: new { apikey = RouteParameter.Optional },
constraints: new { controller = "project" },
handler: new BasicAuthHandler(config));
// all routes requires an api key
config.MessageHandlers.Add(new ApiKeyHandler());
config.MapHttpAttributeRoutes();
}
}
I've tried several "solutions" from the net yet none of them seems to fix this. I've added the:
// Stop IIS/Asp.Net breaking our routes
RouteTable.Routes.RouteExistingFiles = true;
from: http://www.grumpydev.com/2013/09/17/403-14-error-when-trying-to-access-a-webapi-route/
And also made sure that:
<modules runAllManagedModulesForAllRequests="true">
Having the code above, using the following link gives a successful connection where it checks (in the correct order) the APIkey (ApiKeyHandler), checks if the user needs to log in(BasicAuthHandler) and then goes to method in the controller ({controller}/{action}).
// THIS WORKS!
http://localhost:51077/api/v1/project/getprojects?apikey=123456
then we do a publish and tries the same thing
// This is haunted with number 403
http://website.com/api/v1/project/getprojects?apikey=123456
gives the Error Code: 403 Forbidden.
I am clueless. I've even tried changing the whole publish folder's security settings for "NETWORK SERVICE" to full access.. no change.
Let me know if you need any more intel.
Called the web server machine fellas and they had a firewall blocking incoming webapi calls with authenticating. It now works as it should :)

Why does my Web API return only JSON?

I just applied Update 2 RTM to Visual Studio 2013 and started a new Web API project. I have a basic service returning some people objects.
I'm trying to use content negotiation to return JSON or XML depending on the Accept header. Regardless of the Accept header, I get JSON. How do I troubleshoot this and return XML or JSON depending on the header?
Here is the Header from Fiddler: Accept: application/xml
Here is the body of my Get() method:
var patient = this.patientRepository.GetPatients().Where(p => p.Identifier == id).FirstOrDefault();
IContentNegotiator negotiator = this.Configuration.Services.GetContentNegotiator();
ContentNegotiationResult result = negotiator.Negotiate(typeof(Patient), this.Request, this.Configuration.Formatters);
if (result == null)
{
var response = new HttpResponseMessage(HttpStatusCode.NotAcceptable);
throw new HttpResponseException(response);
}
return new HttpResponseMessage()
{
Content = new ObjectContent<Patient>(patient, result.Formatter, result.MediaType.MediaType)
};
Thanks!
Update - Here is the WebApiConfig.cs. I added the line adding the XmlMediaTypeFormatter. By default, it's not included. Either way, the issue is not resolved.
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
GlobalConfiguration.Configuration.Formatters.Add(new XmlMediaTypeFormatter());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}

WEB API. Multiple actions were found that match the request

Need help.
I have 2 controllers:
// POST (Single SMS)
[ActionName("AddSMS")]
public HttpResponseMessage Post(MySMS singleSMS)
{
try
{
SMS_Repository.Add(singleSMS);
return Request.CreateResponse<MySMS>(HttpStatusCode.Created, singleSMS);
}
catch (Exception)
{
return Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Error");
}
}
// POST (Collection of SMSes)
[ActionName("AddSMSCollection")]
public HttpResponseMessage Post(List<MySMS> smses)
{
try
{
SMS_Repository.Add(smses);
return Request.CreateResponse<List<MySMS>>(HttpStatusCode.Created, smses);
}
catch (Exception)
{
return Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "errorus");
}
}
and Route:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional }
Now. If I send request like this:
localhost:25856/api/sms/AddSMSCollection
it works
Is it possible to tune route so that I can use localhost:25856/api/sms and didn't get Multiple actions were found that match the request error??
sorry for my bad english..
You could define the action that should be executed in this case:
defaults: new { id = RouteParameter.Optional, action = "AddSMS" }
But with only the following url localhost:25856/api/sms and not including the action name, I hope you realize that the routing engine has no way of disambiguate which action to execute. The routing engine could use the HTTP verb but in your case both actions are POST.

Resources