MVC Publish to host is adding extra sub folders - asp.net-mvc

I'm publishing an MVC 3 site to a go daddy account. I'm using the FTP Publish utility in VS 2010. The site publishes "okay". But here is the issue:
On my local machine, when I run it the web address is:
localhost/Student/Create
On my go daddy account, I have created a subdirectory called mvctest.
I have created a subdomain called mvctest.mysite.com that points to the virtual directory at mvctest. This works fine.
But after I publish my site to www.mysite.com/mvctest, I am getting an additional /mvctest/ directory in all of my links. What I want is:
mvctest.mysite.com/Student/Create
what I get is:
mvctest.mysite.com/mvctest/Student/Create
What's interesting is that if I manually type in what I want, the page loads fine. If I've hard-coded a link to the root, it's fine, but all of my Url.Content and Html. Links add the additional mvctest directory..
I've searched all over and haven't found an answer to this dilemma. I tried in the web.config, but I don't think that does it. I feel like something in the FTP Publish utility in VS2010 is altering the routing structure in my global.asax file, but I'm not sure.
Any suggestions appreciated.

Is your virtual directory an application? If it's not set as an application, IIS will treat the root of your site as the application and add the subdirectory. See this post.

The following solved the same issue for me and is quoted from a blog post I found (source):
So now for the solution, and I probably should have mentioned this at the beginning but hopefully you’re using IIS7 with the URL rewriting module installed (it is installed by default when you use IIS7 on GoDaddy).
Simply add the following into your web.config’s system.webServer element:
<rewrite>
<rules>
<rule name="Remove Virtual Directory">
<match url=".*" />
<action type="Rewrite" url="{R:0}" />
</rule>
</rules>
</rewrite>
All this does is “rewrite” the URL with itself. This causes URL Rewrite to add the original URL (the one with no folder name) to a ServerVariable which is used by ASP.NET MVC to generate other URLs.

Related

I want to deploy ASP.Net Mvc application inside virtual directory of existing angular6 application

I have an existing Angular website in that I want to deploy ASP.NET MVC application inside the virtual directory. the problem is when I try to route to ASP.NET MVC application inside a virtual directory it shows "403 - Forbidden: Access is denied." error on IIS.
I tried adding a new url rewrite rule in web.config of an angular application as given in
https://www.stefanoscerra.it/iis-rewrite-rules-configuration-angular-web-config/
but it tries to find the angular route and if not match any angular route it redirects to the angular default route
In I was having same scenario like this I solved by adding rewrite rule URL rewrite configuration in web.config .You will need to add rewrite rule like below.
<rule name="mvcinsideangularrule" stopProcessing="true">
<match url="mvcvirtualdir/(.*)/(.*)/(.*)" />
<action type="Rewrite" url="http://yourdomain.com/mvcvirtualdir/{R:1}/{R:2}/{R:3}"/>
</rule>
It will work in your scenario.

MVC bundles and IIS virtual directories (URL rewrite)

I'm hosting multiple applications on IIS server virtual directories and I'm using URL Rewrite to facilitate them. All images and other assets that are manually written like this "~/path/to/my/content" has a correct output "/path/to/my/content", but bundle paths like "~/client/js" gives an output "/myapplication/client/js" which should be "/client/js".
How can I fix that?
How I initiate Script bundle:
var scriptBundle = new ScriptBundle("~/client/js");
Rewrite configuration:
<rule name="Official Website" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" matchType="Pattern" pattern="^(www\.)?domain\.com$" ignoreCase="true" negate="false" />
</conditions>
<action type="Rewrite" url="officialsite/{R:1}" />
</rule>
Was looking into these topics, but couldn't make anything work for me:
How do I get System.Web.Optimization bundles to work with custom folders in an IIS virtual directory?
CssRewriteUrlTransform with or without virtual directory
Why does ResolveBundleUrl not work for custom folders? (MVC Beta 4)
Is it possible to unit test BundleConfig in MVC4?
UPDATE: I'm using Winhost as hosting provider, and they do not support setting up host headers for the IP, probably due to the shared IP. They provide domain pointers to root folders, which is why I'm using URL rewrite.
You said :
I'm hosting multiple applications on IIS server virtual directories
and I'm using URL Rewrite to facilitate them
So, The problem refers to root configuration (Virtual Directory) to be usable for bundling and etc.., It's not about bundling configuration or rewrite rule. I think you need to setup myapplication directory as virtual and set it up as a separate website. Make sure you have followed instructions available in Create Web Site where it says:
you add a Web site in IIS, a site entry is created in the
ApplicationHost.config file. The entry specifies:
network binding for the site
Maps the site to a location in the file system
optionally specifies user credentials for content access
To ensure that user requests reach the correct Web site, you must configure a unique identity for each site on the server.Web sites hosted on the same server can be distinguished using the following unique identifiers.
Host header name (recommended)
IP address
TCP port number
Configure a Host Header for a Web Site (IIS 7) points to the first one.
Update: I think there may be a name conflict on server and your project. more brightly try to change client in ~/client/js and its folder to new one. Unless there were multiple path choices (with this path depth) for server to fetch that you can solve it by adding an extra depth by adding an \ in your project and js bundle mapping.
Update2: Finally I suggest you to follow full article available in How to publish or host subdomain on winhost.com? to solve such this amazing issue :)
I have two options you can try.
1) Try using this to trick it into using the correct path:
var scriptBundle = new ScriptBundle("~/../client/js");
You may get an HttpException (Cannot use a leading .. to exit above
the top directory.), but it's worth a shot. I cannot replicate your
environment, so I cannot test.
2) Create your own virtual path provider:
Changing ASP.net application root?

Azure web site - URL Rewrite using web.config not working

We are migrating some sites over to Azure and I have it setup as a "Web Site".
An example of what we're trying to do is pretty straight forward.
Current URL: http://www.website.com/Products.aspx
Clean URL: http://www.website.com/Products/
So in the web.config, there is the following:
<system.webServer>
<rewrite>
<rules>
<rule name="ProductList">
<match url="/Products.aspx" />
<action type="Redirect" url="/Products" />
</rule>
</rules>
</rewrite>
</system.webServer>
This was generated in IIS. I have tried to set the stopProcess attribute to true... which is a solution that I've seen in a few threads, but nothing is working.
This website is one of our legacy sites that uses an ISAPI module for Rewrites. Since we aren't using an Azure VM, we cannot continue using this method to rewrite.
Is there something I'm missing here?
Azure Web Sites does not have the URL Rewrite module installed, so the config approach won't work. Maybe you could update the site code to use ASP.NET URL routing, as described in this blog post by Scott Gu: http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
Azure Web Sites didn't used to have the URL Rewrite Module installed, but now it does, so you can just use the standard <system.webServer><rewrite> section in your web.config.

MVC Redirect is adding Virtual Directory to URL

My site is hosted on a server with multiple virtual directories, and I have different domain names pointing to their respective virtual directories. My site works fine in debug mode in Visual Studio where nothing unusual happens.
The URL is something like http://www.greatsite.com, but RedirectToAction links that are like this:
return RedirectToAction("Profile", "Account");
I want to go here...
http://www.greatsite.com/Account/Profile
...are going here instead:
http://www.greatsite.com/greatsite/Account/Profile
...where 'greatsite' is the name of the virtual directory.
The site is being published using FTP and the FTP address points directly into the 'greatsite' folder. So the site is published to the root as far as FTP goes.
The page displays either way. The URL points to the virtual directory, and I'm confused why they would both work.
EDIT
I eventually found the answer here:
https://stackoverflow.com/questions/364637/asp-net-mvc-on-godaddy-not-working-not-primary-domain-deployment
So you might want to close this as a duplicate question, sorry. Unless someone can explain this solution in more detail.
<rewrite>
<rules>
<rule name="Remove Virtual Directory">
<match url=".*" />
<action type="Rewrite" url="{R:0}" />
</rule>
</rules>
</rewrite>
RedirectToAction will redirect to another Action/Controller inside the current asp.net mvc application. You can redirect to an external URL using the Redirect method.
return Redirect("http://www.externalurl.com");
or another virtual directory:
return Redirect("http://www.externalurl.com/directory");

How to host an asp.net mvc app on a domain that points to a subfolder?

I have the folowing scenario:
www.somedomain.com -> this points to a folder on a shared host, say /MyFolder1
www.otherdomain.com -> this points to another folder on the same shared host, say /MyFolder2
With asp.net mvc my urls get mapped to:
www.somedomain.com/MyFolder1/Action
www.somedomain.com/MyFolder2/Action
I (obviosly) dont't want to have "MyFolder1" and "MyFolder2" on my URLs. How do i solve this on asp.net MVC?
I want to have:
www.somedomain.com/Action
www.somedomain.com/Action
But i need to keep the subfolders on IIS (or some other solution that allows me to have two sites, with different domains on the same hosting).
Help is very much appreciated.
Thanks
The best way to handle this IMO is to use rewriting at the IIS level. I just did this on a site using IIS 7 URL Rewrite. If you don't have this module installed on your host provider, you can try to use one of the other URL rewriting tools. But, for example on DiscountASP you can use IIS 7 URL rewrite.
First you need to point all your domains to your current site. Then when you download the tool: http://blogs.iis.net/bills/archive/2008/05/31/urlrewrite-module-for-iis7.aspx, it provides a GUI for editing the rules. Ultimately the rules are placed into your web.config file. You want your rules to look something like this:
<rewrite>
<rewriteMaps>
<rewriteMap name="otherdomain" />
</rewriteMaps>
<rules>
<rule name="otherdomain" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="otherdomain.com" />
</conditions>
<action type="Rewrite" url="/site_folder/otherdomain/{R:0}" />
</rule>
</rules>
</rewrite>
If you are using ISAPI rewrite, I'll probably have that one soon as well for another host that I'm using that doesn't support IIS rewrite
My hosting provider is using IIS 6 with ISAPI_Rewrite.
It works fine, www.somedomain.com/MyFolder1/Action becomes www.somedomain.com/Action as i want on the entry page. But all Url.ActionLink() maps again to www.somedomain.com/MyFolder1/Action which i don't want.
The easiest thing to do would be to create a subdomain on the shared host. Something like app1.somedomain.com and app2.somedomain.com, the subdomain maps directly to the subdirectory where the application is and that becomes the root.

Resources