How to set default ApiController in HttpConfiguration.Routes? - c#

the api routing configuration from the template is this:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
I have a public class called
public DataController : ApiController
It works ok with url like ~/api/Data, but I would like to reach Data controller with just ~/api
I tried it like this:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Data", id = RouteParameter.Optional }
);
}
but I get HTTP Error 403.14 - Forbidden
Any solution to the problem?

Related

resource api not found in mvc application

In existing MVC application I have added api support.
Added api controller but when i hit url it shows error -
The resource cannot be found.
I tried url -
https://localhost:44316/api/getdata
https://localhost:44316/Test/api/getdata
TestController.cs
public class TestController : ApiController
{
[HttpGet]
[Route("api/getdata")]
public IEnumerable<string> GetData()
{
return new string[] { "value1", "value2" };
}
}
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
If this is an existing MVC application, then you need to register your web api routes. You need to add this line of code to Global.asax.cs to register your web api routes in Application_Start method:
GlobalConfiguration.Configure(WebApiConfig.Register);
Once you do that, your URLs should then work.
The Route attribute is in wrong place, it should decorate the Controller class.
Try this:
[Route("api/[controller]")]
public class TestController : ApiController
{
[HttpGet]
public IEnumerable<string> GetData()
{
return new string[] { "value1", "value2" };
}
}
and call Get http://localhost:XXXX/api/test.

How to configure the url with a combination of url segmentation and querystring for web api rest url

(using Web api 1.0)
I have a scenario where my action has multiple parameters like
GetCustomer(int id, string email)
where the url would be specified as
GET
api/Customer/{id}/{email}
But i am looking to configured the url as
api/Customer/{id}?email={email}
So it's a combination if url segmentation with query string. Currently when i am trying to set this i am getting this below error.
The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
Parameter name: routeUrl
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Help Area",
"",
new { controller = "Help", action = "Index" }
).DataTokens = new RouteValueDictionary(new { area = "HelpPage" });
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
kindly help me in fixing this thanks in advance
Your configuration needs updating
public static class WebApiConfig
{
public static void Register(HttpConfiguration config) {
config.Routes.MapHttpRoute(
name: "CustomerApi_GetCustomer",
routeTemplate: "api/Customer/{id}",
defaults: new { controller = "Customer" action = "GetCustomer" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
If you make a request to api/Customer/{id}?email={email}
the binder will automatically match the GetCustomer(int id, string email) with the email parameter from the url

Global configuration of Enum serialization to string is ignored

I know that I can set and make all enums to be converted to strings during serialization like this (from JSON serialization of enum as string):
var jsonFormatter = config.Formatters.JsonFormatter;
jsonFormatter.SerializerSettings.Converters.Add(
new StringEnumConverter
{
CamelCaseText = true
}
);
but it seems that is Global ignored in all cases I tried Dictionary<int,List<SomeEnum>>, Dictionary<int, SomeEnum>, List<SomeEnum> or even SomeEnum!
Here is my configuration:
public static class WebApiConfig
{
//https://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome/20556625#20556625
private class BrowserJsonFormatter : JsonMediaTypeFormatter
{
public BrowserJsonFormatter()
{
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
}
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
{
base.SetDefaultContentHeaders(type, headers, mediaType);
headers.ContentType = new MediaTypeHeaderValue("application/json");
}
}
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Formatters.Add(new BrowserJsonFormatter());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// DateTime Formatter
config.Formatters.JsonFormatter.SerializerSettings
.DateFormatString = "o";
// enum Formatter to String
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());
}
}
Any help is appreciated!
As #dbc suggested, putting a config.Formatters.Clear() for the previous formatters and changing the order - made it work.
Here is the final code that converts enums to strings in any kind of data-structure:
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Formatters.Clear();
config.Formatters.Add(new BrowserJsonFormatter());
//enum formatter as strings
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());
// DateTime Formatter
config.Formatters.JsonFormatter.SerializerSettings
.DateFormatString = "o";
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}

Assimilating WebApi Controller in MVC 5 Project

I am proficient in MVC 5 but web api 2 is new to me. I am trying to add a webapi controller to my MVC Project, But I can't access It's path.
Iv'e added a web api controller in a new Folder called webapi, and the name of the controller is searchcontroller, Inside My MVC 5 Project.
I can't access it's path, something that should be:
/api/search/get.
I think It might be with something with my App Start in Global Ajax :
Help Please!
Here is the code in the app start :
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(config =>
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ApiById",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = #"^[0-9]+$" }
);
config.Routes.MapHttpRoute(
name: "ApiByName",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: null,
constraints: new { name = #"^[a-z]+$" }
);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "Get" }
);
});
The controller code :
public class WebAPISearchReultsController : ApiController
{
public IEnumerable<ICE.Logic.ArticleSearchResult> Get(string searchQuery)
{
return SearchLogic.Instance.GetArticleSearchResults(searchQuery);
}
}
I would construct the API method like this...
[Route("api/webapisearchreults")]
[HttpGet]
public HttpResponseMessage Get([FromUri]string searchQuery)
{
var data = SearchLogic.Instance.GetArticleSearchResults(searchQuery);
var response = Request.CreateResponse(HttpStatusCode.OK, data);
return response;
}
You could then check the data and if there's no data or an exception etc you can inspect the HttpStatusCode returned. You can set the status code, to something meaningful.
EDIT: WebApiConfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}

ASP.NET Web API returns code 404 with newly created ApiController

I just recently added a new ApiController to my ASP.NET MVC4 project (I already had one ApiController also in the Controllers folder) called ServerInfoController. I created this using the CRUD template option; here's what it looks like:
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebFrontend.Models;
namespace WebFrontend.Controllers
{
public class ServerInfoController : ApiController
{
// GET api/serverinfo
public Dictionary<string, string> Get()
{
var settings = GlobalStaticVars.StaticCore.LoadServerSettings("Last", "Server");
if (settings != null)
{
return new Dictionary<string, string>
{
{"ServerIP", settings.Address},
{"ServerPort", settings.Port.ToString()}
};
}
return new Dictionary<string, string>
{
{"ServerIP", ""},
{"ServerPort", ""}
};
}
}
}
However, every time that I attempt to navigate to that resource in the browser, I receive a 404 error. I know that my routes are registered correctly as my other controller is still reachable at the /api endpoint.
The following is the route that is configured in WebApiConfig.cs (it's the default route):
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
You don't need {action} part in the route. This is default routing:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}

Categories