Why won't my MVC project work after publishing it? - asp.net

I recently published my first MVC 2 project to a commercial web server, running Windows 2008, and which purportedly supports other MVC sites without issue, but have been experiencing some problems. Here is the high-level structure of the project. As you can see, it is very simple:
But, after the site is published, and I navigate to the URL, I get "HTTP Error 403.14 - Forbidden: The Web server is configured to not list the contents of this directory."
So, I contacted the web host about it and was told I had to include a default landing page, such as Default.aspx, Index.aspx, etc. I doubted this response was accurate because I thought MVC Routing would have taken care of this, but I did as suggested anyway, adding a redirect to my home controller in the Default.aspx.cs codebehind, but got an HTTP Error 404. I added that redirect per advice similar to that found in this article: http://www.58bits.com/blog/CommentView,guid,10b2ddfa-057c-41d0-bdc7-564b212ce896.aspx.
I've been going back and forth with the web host about this for over a week, across about a dozen different responses and answers, yet I have not been able to find a resolution to this. I am sure this is a simple thing to resolve, but I have no idea what to try next, or what to suggest to the web hosting support that they try next.
Soooo ... knowing that the StackOverflow community is smarter than me and the support techs for my web hosting company combined, a thousand times over, I'm hoping you can help me work toward a resolution here, so I can successfully publish my project and access it without error.

There's a few things that could be causing the error:
Issue with the MVC libraries
The production server might not have the MVC libraries stored in the GAC. To fix this, all you have to do is go into your MVC project's References pane/folder and find System.Web.Mvc, System.Web.Routing, and System.Web.Abstractions. Now, select them by Ctrl clicking on them, and set Copy Local to true in the Properties window.
It's hard to know before you publish to a server whether or not this is the case, so I recommend just setting the assemblies to copy local all the time.
Landing Page
Your landing page may have issues. From my experience with ASP.NET MVC, I've seen that a landing page is required for IIS to function correctly. I'm using the page that was included in the ASP.NET MVC 2 template. You should compare mine to yours to see if you have everything that's needed:
Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourNamespace._Default" %>
<%-- Please do not delete this file. It is used to ensure that ASP.NET MVC is activated by IIS when a user makes a "/" request to the server. --%>
Default.aspx.cs:
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
namespace YourNamespace
{
public partial class _Default : Page
{
public void Page_Load(object sender, System.EventArgs e)
{
// Change the current path so that the Routing handler can correctly interpret
// the request, then restore the original path so that the OutputCache module
// can correctly process the response (if caching is enabled).
string originalPath = Request.Path;
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
HttpContext.Current.RewritePath(originalPath, false);
}
}
}
Permissions
Based on the first HTTP status code you got, there may be a permissions problem. The folder containing your MVC application must be defined as an application and set to the appropriate permissions.
It's quite easy to do both those things through IIS. However, you probably don't have access to IIS; if you do, you're very lucky!
Otherwise, you can change the permissions through FTP using the chmod command. You can connect through Filezilla, a very good open-source FTP client, a just do it through a right-click + dialog box.
As for defining the folder as an application, you should check whether you can do it through any of the IIS things provided to you by the host. If not, contact them and ask them to set it up.
Good luck! Hope I helped.

It may be that your production IIS doesn't have the MVC reference files loaded for your site. I ran into the same issue when I loaded a MVC site to a host that said "we fully support MVC." You can try publishing using the "all project files" option. I believe the issue can also be resolved by going into your references folder and selecting the mvc references. I'm not at my developer machine right now but I believe you change a certain property for the references to have them included in your published files.

Try pre-compiling your application. In visual studio setup a Deployment project and then deploy it in release mode.
Because it's pre-compiled you aren't relying on IIS to compile the project for you to run it. This is what I always do these days as IIS on the production server can be a pain if I need to force it to re-compile or update..

Based on the comments and replies above, I ended up exploring wildcard application mapping by asking my web host if they support it. Here is what I got in response:
Wildcard mapping is not supported by our services.
If you would like to use URL re-writing, which is the same as rerouting please use the example bellow.
Example of using URL rewrite map where the following URL
/default.aspx?id=1 will be replaced with /default.aspx?id=25
<rewrite>
<rewriteMaps>
<rewriteMap name="/default.aspx?id=1">
<add key="1" value="25" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite rule1 for /default.aspx?id=1">
<match url=".*" />
<conditions>
<add input="{/default.aspx?id=1:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
If what you are trying to do is not URL re-writing but is URL redirection please use the example bellow.
To redirect your web site using ASP create file "default.asp".
Place the following code inside:
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.new-url.com/"
%>
To redirect your web site using HTML:
<HTML><HEAD>
<META HTTP-EQUIV=Pragma Content="No-Cache">
<META HTTP-EQUIV=Refresh Content="5;URL=http://YOURURLWHEREYOUWANTTOREDIRECT">
</HEAD>
Will any of the stuff above even work?. Seriously, should it be this hard publish an MVC project? I'm really starting to get frustrated ... :(
Should I just look for a new web host? Would that make a difference?

The following request is sent:
http://stackoverflow.com?variableValue=87
redirect page using variable values in a Query String
:-
Dim variableValue
variableValue = Request.QueryString("Occassion")
if variableValue = 87 then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.google.com"
end if

Related

Routing an ASP page to a Action in ASP.NET MVC

I am recreating a site from VB .NET to MVC 4 ASP.NET. In the old project there is a system that sends an email to a user and within that email there is a hyperlink that can direct the user to view the specific task. I want to make all of the old emails work with the new recreated site. Here is an example of the hyperlink in the old system:
http://pppweb/accounting/ap/default.asp?etaskid=32698
And here is an example of what a hyperlink looks on the new system.
http://pppweb/accounting/ap/ApIssues/Task/32698
So I was thinking i could create a default.asp page in my MVC project and try and have it parse the taskID out. I am not exactly sure the best way to go about doing this?
I would advise against adding a classic .asp (not .aspx?) page in your project, since I'm not even sure there is a way to do that. You can, assuming you are running IIS7, map the url:
http://pppweb/accounting/ap/default.asp
to be handled by MVC by adding the following line to your web.config file:
<configuration>
...
<system.webServer>
...
<handlers>
...
<add name="aspnet htm" path="account/api/default.asp" verb="*" type="System.Web.Mvc.MvcHttpHandler" preCondition="integratedMode" />
Then, add a route for that url and requests should resolve to the normal route parameters you specify. From that point, you should be able to access etaskid from the query string easily enough.
Edit:
Alternatively, if the url matters a lot to you, you can specify a redirect rule from the old format url to the new one by using UrlRewrite module (also an IIS 7 feature). You can read about it here: http://www.iis.net/downloads/microsoft/url-rewrite

IIS / ASP.NET: One URL returning 403 on one server despite other URLs in site working fine

I have taken over the maintenance of a production website at a new job that is written in ASP.NET 4 Webforms and that runs on IIS 6 on Windows Server 2003. I am not familiar with Webforms nor managing IIS...so I am kind of working things out as I go here.
I have done several deployments to our production server which have worked fine, but am now setting up a test environment that is identical, just a different IP address/domain, so we can properly test changes first.
I have a problem where on this test site any URL that does NOT end with a reference to a file (always .aspx on this site) will return a 403 error on this server. For example http://users.test.oursite.com/admin will always return a 403 error when logged into the site. It should be redirecting to http://users.test.oursite.com/admin/organisation.aspx. Having an MVC background I am not even sure how this happens...but it does in production.
Browsing links within the site is fine as they always reference an .aspx file. Manually typing URLS that reference an .aspx file is fine, just not when the URL does not contain a file. This is not a problem on the production server.
As I said, I am not familiar with WebForms or managing IIS itself...so I have kind of run out of places to look.
Is there anything that comes to mind that I should be looking at that could be causing this problem?
In WebForms typically there is no routing involved.
You need to either provide a full path on the URL ending with .aspx OR
setup default documents for your website. (Index.aspx, Default.aspx etc.)
http://www.iis.net/configreference/system.webserver/defaultdocument
OR
<configuration>
<system.webServer>
<defaultDocument enabled="true">
<files>
<add value="home.html" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Default documents can be setup in IIS Properties or via web.config.
Otherwise you need to provide the full path.
And because you haven't setup the DirectoryBrowsing (in IIS), you get a 403 Error when you try to access the Directory.
Enable the Directory Browsing option in IIS (not recommended) if you want this error to go away.

httpModule for 404

I've created an httpModule to handle URL remappings, and it works great on my test system. A request for www.mydomain.com/Some_Fancy_URL gets rewritten to www.mydomain.com/some.aspx?fancy=23 and so on.
When I deploy to the actual web site, I'm getting the default IIS 404 page though.
After doing some research online, it would seem that I need to setup "Wildcard Mapping" in IIS 6 to get the request past IIS and in to my httpModule. The problem is that the site is hosted on a shared server, so it may not be possible to get the ISP to make that change.
My question is, can't I use an httpHandler to tell IIS how I want these requests handled? For example:
<httpHandlers>
<add path="*.aspx" verb="GET,POST" type="System.Web.UI.PageHandlerFactory" validate="false"/>
</httpHandlers>
It would seem like adding this to my Web.Config should tell IIS to stop validating the existence of .aspx files, and just pass the request along for me to process. It doesn't work though.
Any suggestions?
The problem with IIS 6 and ASP.NET is that they're aren't integrated. IIS needs to be told about ASP.NET via script mappings (.aspx, .asmx, wildcard and so on).
None of your web.config configuration settings will influence IIS because web.config is there to configure ASP.NET's behaviour, not IIS. IIS has no knowledge of web.config.
Unless you can hand off a request to the ASP.NET pipeline (via a script map) nothing will happen and all your web.config settings will be ignored.
With IIS 7 the story is quite different. In IIS7, ASP.NET and IIS are closely integrated and share the same pipeline thus permitting you to achieve the result you're looking for.
The alternative may be to find out if your hoster runs a URL rewriter such as ISAPI_Rewrite on their servers. That way you could rewrite urls without having to map a wildcard scriptmap to IIS6.
Through some trial and error, along with more web searches, I found a solution. It essentially parallels Kev's answer.
IIS won't treat a request as .NET unless it has a known file extension (.aspx, .ascx, etc.). When I send along something like www.mydomain.com/anything it looks for a file or folder named "anything", and when it doesn't find one, it just drops off to the default IIS 404 handler.
That's where I took over. I changed IIS 6 to forward 404 problems to /404.aspx. I then created that page with a generic "Your file wasn't found" message in the same style as my web site.
Here's the good part: Now that IIS is sending 404's to a .NET page, the custom httpModule I created is getting fired. The request is for 404.aspx, but IIS is nice enough to also append the original URL as well. You get something like:
www.mydomain.com/404.aspx?404;http://www.mydomain.com/anything
This allows me to parse the request in the httpModule, and rewrite as needed!

web.config urlmapping

I have a comment form on my website on contact.aspx. I want to be able to put up a redirect from /comment (no extension) to point to contact.aspx.
I set up the following url mapping in my web.config and when I test locally it works fine. When I post it to production, the redirect doesn't happen and I get the IIS 404 error.
<system.web>
<urlMappings enabled="true">
<add
url="~/comment"
mappedUrl="~/contact.aspx"/>
</urlMappings>
I'm assuming this is because IIS isn't serving up the request to the asp.net engine and I'm using a shared hosting environment (discountasp.net) so I don't have direct control over IIS to configure it there. I can always put in the subfolder and a default.aspx that will redirect for me, but I thought I would inquire about this route first.
What does the collective think?
Ask your web host if they can configure your virtual directory to serve all requests through ASP.Net.
Many hosts (mine included) are willing to do this. (Mention ASP.Net MVC, which requires this)

UrlRewriting.Net Module + IIS7 Equals Page.User == null?

I've used the UrlRewriting.Net module for a couple years now without any problems in Windows XP and Windows 2003. I just recently upgraded my home PC to Windows 7 and started developing a new website.
The plan was to use .html extensions and rewrite them to their .aspx counterparts using the UrlRewriting.Net module. Everything works flawlessly in VWD 2008, but when I try running it through IIS7 it is a different story.
When I try to access a page via the .html rewrite I can no longer access Page.User; it keeps returning null. If I hit the page using it's .aspx extension, Page.User is correctly populated. I should also mention that I have a LoginView controller in my Master Page and it suffers from the same symptoms: When accessing via .html extension it shows the AnonyousTemplate; When using .aspx extension it properly shows the LoggedInTemplate. I'm guessing the two are related.
[Note: I've also tried extensionless URLs and they exhibit the same problem]
The only way I've gotten it to work is to switch the application pool to Classic, which then requires me to add an ASP.Net ddl handler for the .html extension [otherwise it is handled by the StaticFileHandler and comes up as a 404 error]. However, I'd like my web app to run properly for people without having to fiddle around with IIS.
So I am left with several questions:
Does anyone have ideas as to why Page.User always equals null for .html => .aspx rewritten pages?
Why does it work in VWD 2008, but not IIS7?
What changed from IIS6 => IIS7 that could have caused this?
Any other thoughts on workarounds?
[Note: I just tried a .aspx => .aspx rewrite and it did not exhibit the problem. Not really what I want, but thought I should mention it.]
Just had a breakthrough with the UrlRewriting.Net module. This makes it work in Integrated Mode in IIS7:
<modules runAllManagedModulesForAllRequests="true">
After figuring it out I did a search on "runAllManagedModulesForAllRequests" and the first thing that popped up was Scott Guthrie's blog which actually talks about using it for this purpose.
Another approach that seems to work is to remove the Session module and readd it leaving the "Invoke only for requests to ASP.NET applications or managed handlers" checkbox unchecked. It looks like this in the web.config file:
<system.webServer>
<modules>
<remove name="Session" />
<add name="SessionManualAdd" type="System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
It seems the problem is that the Session module doesn't execute for say '*.htm' files when HttpContext.RewritePath is used, but removing and readding the module in this fashion causes the Session handler to be executed for the request.
This solution was suggested on the thread below. Unfortunately Microsoft chose not to explain the reasoning behind this behavior fully:
http://connect.microsoft.com/VisualStudio/feedback/details/357248/context-rewritepath-disables-session-module-in-iis7
Microsoft included a fix for this issue (at least for extensionless urls) in Service Pack 1 for Win7 and Windows Server 2008 R2:
http://www.microsoft.com/download/en/details.aspx?id=5842
Also available as a hotfix: http://support.microsoft.com/kb/980368
After this patch is applied, ASP.NET 4 applications can handle requests for extensionless URLs. Therefore, managed HttpModules that run prior to handler execution will run. In some cases, the HttpModules can return errors for extensionless URLs. For example, an HttpModule that was written to expect only .aspx requests may now return errors when it tries to access the HttpContext.Session property.
After applying SP1 or the hotfix, no web.config changes are needed to make the session and forms auth work for extensionless URLs rewritten to asp.net pages/handlers/etc.
I don't know if this fixes anything for rewrites to static file extensions like .htm. My guess is, probably not. I would try to avoid setting runAllManagedModulesForAllRequests="true" in production environments, because it adds unnecessary overhead on static file requests.

Resources