WebAPI - Attribute routing to remove virtual directory from URL - asp.net-web-api

I am using WebAPI 2 with Attribute routing in my project. I deployed my project under a virtual directory in IIS. I need to remove virtual Directory name from the URL.
Now my URL is api.mydomain.com/virtualdirectory/{controllerName}/{Version}
In my Route Prefix I am passing controllerName/Version.
I need to change the URL to
api.mydomain.com/{controllerName}/{Version}
Please help me how can i achieve this using attribute routing.

The question is will a request like api.mydomain.com/{controllerName}/{Version} hit your action?...route templates(with conventional or attribute routing) are always relative to the base path of your application and if the base path of the application includes the virtual directory, then the request urls would need to have this information to hit the actions...one option i can think of is to host your application directly under the website...

Related

What does it mean to make URL relative?

My project is build using Angular and Springboot. In environemnt.prod.ts file I have backend url.
I am not sure what does it mean to make url ralative so that it should use same server url?
enviroment.prod.ts file
production:true,
url:'http://localhost:8080/api/amazon/steven'
The relative URL does not use the full web address and only contains the location following the domain. It assumes that the link you add is on the same site and is part of the same root domain.
Update environment.prod.ts file
production:true,
url:'/api/amazon/steven'

Laravel website behind the reverse proxy and subfolder?

I have a Laravel website with LAMP stack and host on a ec2 server. I setup the Virtual Host configuration for the host name www.abc.com and it works fine. One day, The client said they want to use their domain to visit the website like www.client.com/abc. They use the reverse proxy to point our ec2 server. The first problem we met is the virtual host has failed and we fixed it by adding server alias setting.
ServerAlias www.client.com
The url has connected to the server correctly and the second problem is coming up. Because of the subfolder of the url www.client.com/abc, all the assets paths are failed. For example, we had fonts, styles and js paths that generated by laravel-mix:
{
"/js/app.js": "/js/app.js",
"/css/app.css": "/css/app.css",
"/js/login.js": "/js/login.js"
}
Those paths are absolute and it turns to 404 found on the website if we use the mix() helper function to load assets. The rendered url on the blade pages are wrong.
It would be
http://www.client.com/js/app.js
instead of
http://www.client.com/abc/js/app.js
And the fonts loaded from the vendor and styles, which use the relative paths and are converted to absolute as well. So we had to workaround this by adding the option processCssUrls: false to webpack.mix.js.
The third problem is the redirection when user logs in out. The defaults redirect url is /. I know we can change the redirect urls in middleware and controllers but it feels like so much workaround over this. Is there any better way to solve those problems?
To declare the question again, how to setup a Laravel project behind the reverse proxy with subfolder?
http://www.example.com/laravel

Need help in pointing application in the default website to have a domain name

How to add an IP address to the virtual directory in IIS, for example i have pointed the domain name to the default website and it is pointing correctly, the issue i would to have a domain for the defaulvirtualdirectory/application(ex:www.xyz.com/abc) to have a domain name http://abc.def.com.
I tried all the possibilities like HTTP redirect but the problem is it is pointing to the default website instead of application.
If you want to have website URL like http://abc.def.com to load web-contents of sub-directory e.g WWWROOT/application, you should add another subdomain website http://abc.def.com with documentroot as WWWROOT/application. The primary website(http://def.com) will load web-contents of WWWROOT and subdomain website(http://abc.def.com) will load webcontents of WWWROOT/application directory. You can also point subdomain website to different IP address if you want.

How to remove directory name from url with global.asax code

I am using MVC routing in asp.net Web forms and I want to remove the folder/directory name appended in the URL. Locally this works fine but I have the following address now appearing on Godaddy shared hosting.
www.mywebsite.com/my-folder/contact
but I want like this
www.mywebsite.com/contact
I have tried web.config techniques but no success that's why I am asking is there any way to redirect this using Global.asax.

why my IgnoreRoute does not work?

I want to prevent users access for my "~/Content/..." folder I wrote it as follow in "Global.asax.cs" and put this line of code at the top of every other routes
routes.IgnoreRoute("Content/{*pathInfo}");
but it does not work. in fact user can see every files in content folder by type the URL in browser.
am I missing something?
How did you figure out that it does not work? Give example.
You may have put it last in the Routing table. So try to move it up so that it gets added to the routing table first. The route collection is an ordered list of routes.
Also try this : Routes.IgnoreRoute("Content/");, but your version of ignore is also correct and it should work.
Lastly, I do not know what you mean when you say the user can see all the contents of the Content folder : Isn't that the point? User must be able to download files from the folder, and we usually just need MVC to ignore the requests from coming into the framework, and so that IIS can directly serve those files.
or did you mean Directory browsing is enabled, and you want to disable that : In that case go to IIS manager, and select your website and look for the Directory browsing option and disable it as shown here.
Your problem cannot be solved by routing constraints. There are 3 significant steps in processing request:
IIS got request.
IIS watch at filesystem and search for direct correspondence to file
If IIS didn't found any file - it gives request to ASP.NET MVC for processing.
So, you need to configure folder security to forbidden direct access to files, but allow access to application, as here.
But I don't recommend to secure folder, that should be shared. I don't believe that your site shouldn't have images to display :) If you have some secured content, you need to create another folder.

Resources