How to publish assets using web-api - c#

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).

Related

API Calls to controller is breaking after deploying to IIS

I am invoking API calls to get JSON data from server using javascript.
$(function () {
var customersTable = $('#Customers');
var returnCustomersTable = UpdateDataTable(customersTable, "/Customers/Loaddata");
}
This works fine when I am working with Visual studio Dev environment , because my web application is on root and so all javascripts Works fine.
Example:
My WebSite URL: http://localhost:4391
API Calls will be: http://localhost:4391/Customers/Loaddata
This works fine.
But when I deploy application to IIS, my website URL will be,
My WebSite URL: http://localhost/MyAppName
But API calls would still be,
API Calls will be: http://localhost/Customers/Loaddata, which results in not found.
Since I am using javascript in a separate file , I wont be able to do URL.Action.
The other option would be to create a base URL for dev and prod and the append with each servcie call.
But I am thinking if there is any other way.
The routing configuration in ASP.NET MVC is designed to serve as the single place in the application where all of the URLs can be maintained.
It helps considerably with maintenance of the project if all URLs are generated using routing through one of the UrlHelper based methods, such as Url.Action() rather than hard coded in controllers and views.
As long as your JavaScript is in an MVC view, you can just resolve Url.Action inline.
$(function () {
var customersTable = $('#Customers');
var returnCustomersTable = UpdateDataTable(customersTable,
'#Url.Action("Loaddata", "Customers")');
}
If the JavaScript is in a separate file outside of MVC, consider passing the URL through a variable or function parameter.
This routing feature also makes it easier to deploy ASP.NET MVC, because the generated URLs will adapt to use the application path when the application is not deployed to the root website virtual directory.
Application running in IIS web site root:
/Customers/Loaddata
Application running in virtual subdirectory configured as IIS Application named "MyAppName":
/MyAppName/Customers/Loaddata

How to add a Web API service to a non-MVC Asp site?

With the help of several online tutorials, like this one, I am still struggling to add a Web API service to an existing Asp site, that is not MVC.
I added to the project a new item of type Web API Controller Class(v2.1), named it something like AbcController.cs, and VS2015 asked me to put it in the App_Code directory. The default code has handlers for Get, Put etc. Sounded to me like I am on the right track.
I added a default route in Global.asax.cs like in the tutorial:
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
This got built after adding a reference to System.Web.Http.Webhost which was not mentioned in the tutorial. Sounded like I was still on the right track.
However, it doesn't work. I run the site in debug and this gives me a 404 Not Found:
http://localhost:54905/api/abc
I tried to run this on the production server with IIS7, of course as a second test web site to not interfere with the version that is in production. However, I ran into the error that the Microsoft.Web.Infrastructure dll could not be found. To fix this, I should install MVC packages, which I don't like for just an experiment.
My questions are:
do I get it right that the URL is in lower case, i.e., not .../api/Abc ?
does this kind of routing work in the debugger?
am I essentially turning the web site into an MVC web site?
is this really the simplest way to add a "REST" service to an existing web site? I only need to implement the POST, read and return some JSON data, and do not need arguments in the URL

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 configure ActionLink format to drop the subdirectory?

Let's say the domain that is mapped to my root hosting directory is example.com. GoDaddy forces mapping of other domains to subdirectories of the root. For example, my second domain, example1.com, is mapped to example.com/example1.
I uploaded my ASP.NET MVC site to such a subdirectory, only to find that ActionLinks that are for navigation have the following format:
http://example1.com/example1/Controller/Action
In other words, even when I use the domain that is mapped to the subdirectory, the subdirectory is still used in the URL. I want to change the format of my ActionLinks.
However, I noticed that I can also access the same path by going to:
http://example1.com/Controller/Action
(leaving out the subdirectory)
I want to have my ActionLinks automatically drop the subdirectory, as it is not required.
Is this possible without changing the ActionLinks into plain-old URLs?
No, I don't think so, as an action link mainly works to render the controller/action. The other work around, if you have install access for the server, is to use an URL Rewriting tool like iirf.codeplex.com, which is free and works pretty good. There may be some other unintended consequences with rewriting though, depending on what you are doing.
HTH.
You could try adding additional route statements in your global.asax, in order from your subdirectories. Example:
routes.AddRoute("example1/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.AddRoute("{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
I believe the routes are checked in order from the global asax, so you could effectively route the request to the right spot. However, your link would still contain the 'example1' folder in the URL.

Why isn't my Route.Ignore working on this static file in my ASP.NET MVC application?

Background
I have a PDF file located (under my Project) in an Assets > Documents folder:
When my application gets deployed, it gets deployed to a particular folder on the domain. For example, http://www.domain.com/MyAppFolder. I want to be able to access this PDF file by linking to http://www.domain.com/MyAppFolder/Assets/Documents/EZTrac_UserGuide_NewSys.pdf
Problem
I can't seem to get the routing correct for this, as it keeps trying to route this request to a controller. Here is the modification I made to the routing:
routes.IgnoreRoute("MyAppFolder/Assets/Documents/EZTrac_UserGuide_NewSys.pdf");
But this is the result that I get:
The IControllerFactory
'EZTrac.DependencyResolution.ControllerFactory'
did not return a controller for a
controller named 'Assets'.
Try removing the MyAppFolder from your routes.
routes.IgnoreRoute("Assets/Documents/EZTrac_UserGuide_NewSys.pdf");

Categories