AspNet Core ignore virtual directory path - c#

I have a hybrid web application that has WordPress as the root site with an aspnet core web application as a virtual directory. The IIS structure looks something like:
+- DefaultWebSite (hosts WordPress)
| +- web (virtual direcotry that hosts the aspnet core site)
Within the aspnet core site, when I redirect to a page or use tag helpers the url includes "/web/blahblah" which makes sense. However, I don't want that. How do I force the aspnet core site to ignore the virtual directory path and generate urls like "/blahblah" without web?

You can add the routing middleware in such a way that it knows where the controller name comes in the URL.
For Ex. if your asp.net core root url is - http : // www.mysite.com / web
You can configure routing like below:
routes.MapRoute(
name: "default_route",
template: "/web/{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });

Related

How do I create pages in a web api project?

I want to display a default status page for my web api project (where instead some IIS message is displayed when I start the project). However it seems like I cannot create views in web api (there is no support for ActionResult).
In addition to a status page I will also use this information to create an api documentation page.
How can I achieve displaying html pages in this situation ?
If your default status page is static html, you don't have to use MVC. Just tell WebApi in your Startup.cs that you want to support static resources:
app.UseFileServer();
For creating an API documentation, maybe you could write that file on startup dynamically?
You can create regular controllers and views in a webAPI project the same as any MVC project. Just create a normal controller that does not inherit from ApiController. In your startup.cs make sure to configure at least a default route.
configuration.Routes.MapHttpRoute(
name: "someName",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Right click and select Add, then you should see controller at the top. Select one of the mvc controllers.

ASP.NET Web Api - Target URI and deployment

So I'm brand new to developing an API, I have an existing MVC website which is published on Azure, and I would like to create my API using the ASP.NET Web Api template in a new project.
My question is in deployment. How can I deploy this API so that the target address is like:
www.MyMVCWebsite.com/api/etc...
I don't want to modify my existing MVC site to contain the api as I've seen some posts mention that it is less secure, and all tutorials I've seen are for the web api template and I'm learning this from scratch.
Within the publish to azure settings in visual studio it gives the option to publish to an existing site...would this achieve what I want or is there more to it?
JK
You just need to configure the routes to what you want it to be.
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
See Here

Web api routing in subfolder application

I have a main site running under example.com. Now I'm creating a application into example.com subfolder like example.com/subfolder/subsite/, so I have created a application in the subsite folder and everything is working fine, except the routes.
I have the following route:
RouteTable.Routes.MapHttpRoute("myapi", "api/{controller}/{hash}", defaults: new { hash = RouteParameter.Optional });
The route is working fine if I'm debuging the API in localhost or hosting it somewhere else in the root, but it's not working when running inside the subsite application folder.
Any tips?
Did you really create a new web application instead of just a virtual sub directory?
I just tested it, create a simple api controller which returns a string and create a /test site in a /test/sub site, both running the same web api project.
And it simply works.
If this is not the issue, please provide more details... error msg etc...

How to publish assets using web-api

I am using a MVC Web Api server application for generating client data. In addition, I want to publish a collection of JavaScript, XAP (Silverlight) and XML files to my client-side application. Currently, I have a project structure in which those directories are mixed through my .Net implementation code (what I do not like), as the client app uses URIs to request these files can not be changed. Nevertheless, I want to separate the client data from the server application implementation in a different folder.
Therefore, is there a way to store the client data in a separate folder, e.g.
/clientdata/JavaScript
/clientdata/XAP
/clientdata/XML
/clientdata/...
in my project, while still being able to access these files using URIs like
/JavaScript
/XAP/
/XML/
Which are the URLs used by the client app and again can not be easily changed.
This is the main method of my Global.asax
AreaRegistration.RegisterAllAreas();
RouteTable.Routes.MapRoute(
name: "Default",
url: string.Empty,
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "TCMsimulator.Controllers" }
);
var config = new Microsoft.ApplicationServer.Http.HttpConfiguration();
RouteTable.Routes.MapServiceRoute<ResourcesService>("resources/", config);
RouteTable.Routes.MapServiceRoute<PublicResourcesService>("publicresources/",config);
RouteTable.Routes.MapServiceRoute<MonitorService>("monitor/", config);
Is there a way to add a folder redirect to the routing table, such that a virtual folder in the URI is redirected to a filesystem folder? Like can be done using ModRewrite in Apache? It seems like a simple problem, but I have not found a solution to it in Web Api.
Thanks in advance!
I recommend using Attribute Routing. It is much more understandable. You can see examples of multiple routes mapped to a single resource.
http://attributerouting.net/
If these directories only contain files and not Web Api generated output, you should probably ignore the routes to the directories and make sure the requests for them run outside of Web Api, like regular static files that are served from IIS (e.g. images).

Controller requires lowercase in MVC3

I added a new Controller called "ListController" in to MVC project
if I write in browser Url http://localhost:1509/list everything works fine
But if I write in browser Url "http://localhost:1509/List" I get Erorr page
The only difference is if the letter L in the URL is capitalized or lowercase letter
What causes it
Either case sensitive routing has been configured in the global.asax file without you knowing about it, or you are missing something.
Asp.net mvc routing urls are not case sensitive by default. "home", "HOME", "hOmE", etc.. are all the exact same as far as the mvc routing engine is concerned.
Here is the url where ScottGu explains the Asp.Net MVC url Routing Rules how they work.
ScottGu - ASP.Net MVC Framework: URL Routing

Categories