简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM