I am using the code in the below link to create a web api.
https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
I am using only the Products controller and Products code from the link and not using the UI related code. With local docker container, it is working fine.
starting Point:
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}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
When I deploy the same in AKS and try to access the api, it is giving an error.
I am using the url "https://{service external ip}/api/products".
Can someone help me to connect to the AKS in the right way if I am doing any mistake.
Related
Trying to host locally a rest service in IIS. Everything works fine in IIS Express but as soon as I try to host locally in IIS the routing doesn't work.
It's compiled to x86 and is hosted inside of an app pool that allows for 32 bit processes.
I have the site mapped to the binary output folder from my build msbuild /p:Configuration=Release /p:ProcessorArchitecture=x86 -r:False
I have Anonymous Authentication enabled
Binding is type http to port 8000
I have enabled directory browsing and see the below at http://localhost:8000/
However whenever I try to navigate to http://localhost:8000/api/HeartBeat I get a 404 - Not Found.
My WebApiConfig class which sets a default route.
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}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Here is the controller
public class HeartBeatController : ApiController
{
// GET: api/HearBeat/5
public HttpResponseMessage Get()
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
If I run this in IIS Express and navigate to that same url I get a 200. Also I've tried to examine the URL in powershell using Get-WebUrl and it shows a ProtocolError but I have no clue what or why. And IIS Manager doesn't seem to complain about anything.
PS C:\Windows\system32> Get-WebURL -PSPath "IIS:\Sites\Test" | Format-list
Status : ProtocolError
ResponseUri : http://localhost:8000/
Description : Forbidden
A couple of things were amiss.
Publishing via Visual Studio is quite different than the build output that was created via the build command above.
I was missing under Windows features/IIS/Application Development Features
Finally was still getting a security error which required adding an ACL rule to the directory
I read in article that web api 2 can be easlity be integrated in Owin pipeline.
http://johnatten.com/2015/01/11/asp-net-web-api-2-2-create-a-self-hosted-owin-based-web-api-from-scratch/
// Owin code:
public class Startup
{
public void Configuration(IAppBuilder app)
{
var webApiConfiguration = ConfigureWebApi();
// Use the extension method provided by the WebApi.Owin library:
app.UseWebApi(webApiConfiguration);
}
private HttpConfiguration ConfigureWebApi()
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"DefaultApi",
"api/{controller}/{id}",
new { id = RouteParameter.Optional });
return config;
}
}
I've read a lot articles about OWIN specification, but I'm still a little confused, please help me to clarify the questions:
Q:
Is it possible to ingtegrate asp.net mvc5 to Owin pipeline in the
same way as Web Api?
Or it is not possible due to tight binding to system.web assymbly?
No unfortunately it's not possible to run ASP.Net MVC 5 on Owin/Katana or self host. It needs IIS.
But ASP.Net Core completely runs on Owin and selfHost.
I am doing an MVC5 Web API Application. I am doing an simple example.
Create an Web Asp.Net web Application.
Select Empty and API.
Then I add a Api2 Controller called Home, and add a Simple Method called Get()
Method Get() looks like this.
public string Get()
{
return "Hello World";
}
I run the application and complete the URL.
http://localhost:56464/Home/Get
Got an error
Error HTTP 404.0 - Not Found
I test changing WebApiConfig adding
{action}
but I get the same error.
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 }
);
}
When I start the Application http://localhost:56464/, I got this error
Error HTTP 403.14 - Forbidden
I always run the Application from Visual Studio 2013. I did not publish it it IIS
What is missing?
There are a few issues.
First the web api route template is
api/{controller}/{action}/{id}
Note the api prefix.
So that would mean that you have to browse to
http://localhost:56464/api/Home/Get
http://localhost:56464/ wont work because the route has the api prefix. So the forbidden error is default for what ever is hosting at that address.
To be able to use the URL you want in the question you would need to change the route template to match your desired template.
I have a web api service placed on https sub domain. I used castle Windsor to create all classes (per web request), from controller to domain objects.
I tested all actions on local machine and also using fiddler. everything works fine. And no memory leak was happened. and also we have a mobile programmer which worked with all actions and again every thing was fine.
but after hosting on https, by each request to web api urls, application pool stops.
but the request to authorization works fine. What is wrong with my application? here is my code to Config file:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config, IAppBuilder app)
{
// Web API configuration and services
var container =Bootstrapper.WireUp();
ConfigCors(container, config);
RegisterControllers(container);
UserManagement.Config.Bootstrapper.WireUp(container);
config.Services.Replace(typeof(IHttpControllerActivator), new WindsorControllerActivator(container));
container.Register(Component.For<IDataProtectionProvider>().ImplementedBy<DpapiDataProtectionProvider>()
.UsingFactoryMethod(x => app.GetDataProtectionProvider()));
// Web API routes
config.MapHttpAttributeRoutes();
config.AddFiveLevelsOfMediaType();
config.Routes.MapHttpRoute(
name: "DefaultApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//config.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = #"\d+" });
}
private static void ConfigCors(IWindsorContainer container, HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
}
There is no specific reason that application pool is stopping. It could be a windows registry error, system error or assembly permission error. The best way to find out why is to go and check event viewer.
From Administrative Tools select Event Viewer
Alternatively, press Windows Key and type Event Viewer. Once there look into the into Windows Logs and then Application logs. Search for any error (red icon) or warning (yellow icon) that is related to IIS / ASP.Net or .Net framework. I have attached a sample image from my system -
Another Instance -
** EDIT **: There is one other possibility since it stopped after enabling https. Check the SSL certificate. The most common reason is using same certificate in multiple sites. Only wild card certificates can be used in multiple site. If the certificate is self signed, delete it and create a new one with * , being the prefix for the certificate name. Then use that one.
I am trying to add a REST API to a console application using self hosted Owin Web API 2.
// Starting the REST API
string baseAddress = "http://localhost:8000/";
WebApp.Start<WebApi.WebApiStartup>(url: baseAddress);
I am relying on previously instantiated objects to access a third party API (used in the console application).
Since the Web API mimics the console application behavior it should use the same objects. Thus my question: How do I pass those objects to the constructor of the web app?
Once the webapp started I use unity to Inject the objects to the controllers:
var container = new UnityContainer();
container.RegisterInstance<AggregatePosition>(position);
HttpConfiguration config = new HttpConfiguration();
config.DependencyResolver = new UnityResolver(container);
config.Routes.MapHttpRoute(name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
in this example the object "position" should be passed to the web app at initialization. Is there a way to Inject it there?