Add default route in IIS - asp.net-mvc-3

When I deploy my MVC3 web site on IIS 7.5 and click on browse (on port 80), my browser display the web site with localhost url (or serverName url) but my default route is like that :
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{lang}/{controller}/{action}",
new { controller = "Home", action = "Index", lang = Thread.CurrentThread.CurrentUICulture.ToString().Substring(0,2)}
);
}
Is there an mean in IIS to have full url when I click on browse, i would like to automatically add /fr/Home/Index.
Thanks

Add this code in you web.config
<system.webServer>
<defaultDocument>
<files>
<clear />
<add value="Path of your Page" />
</files>
</defaultDocument>

Related

rewrite URl from www.m.example.com to m.example.com

I am trying to do a redirect in ASP.NET web.config which contains like www.m.example.com to m.example.com; I have tried different approaches but wasn't able to do it.
Instead of the web.config, you could apply the following to your page:
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-location.com");
}
</script>
Read more here
If you do still want to use the web.config file, you can do the following:
Open web.config in the directory where the old pages reside
Then add code for the old location path and new destination as follows:
<configuration>
<location path="services.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://domain.com/services" httpResponseStatus="Permanent" />
</system.webServer>
</location>
<location path="products.htm">
<system.webServer>
<httpRedirect enabled="true" destination="http://domain.com/products" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>

ASP.NET MVC Custom Error Pages with Magical Unicorn

my question is regarding Pure.Kromes answer to this post. I tried implementing my pages' custom error messages using his method, yet there are some problems I can't quite explain.
a)
When I provoke a 404 Error by entering in invalid URL such as localhost:3001/NonexistantPage, it defaults to the ServerError() Action of my error controller even though it should go to NotFound(). Here is my ErrorController:
public class ErrorController : Controller
{
public ActionResult NotFound()
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.NotFound;
var viewModel = new ErrorViewModel()
{
ServerException = Server.GetLastError(),
HTTPStatusCode = Response.StatusCode
};
return View(viewModel);
}
public ActionResult ServerError()
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var viewModel = new ErrorViewModel()
{
ServerException = Server.GetLastError(),
HTTPStatusCode = Response.StatusCode
};
return View(viewModel);
}
}
My error routes in Global.asax.cs:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
name: "Error - 404",
url: "NotFound",
defaults: new { controller = "Error", action = "NotFound" }
);
routes.MapRoute(
name: "Error - 500",
url: "ServerError",
defaults: new { controller = "Error", action = "ServerError" }
);
And my web.config settings:
<system.web>
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/ServerError">
<error statusCode="404" redirect="/NotFound" />
</customErrors>
...
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/NotFound" responseMode="ExecuteURL" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" path="/ServerError" responseMode="ExecuteURL" />
</httpErrors>
...
The Error views are located in /Views/Error/ as NotFound.cshtml and ServerError.cshtml.
b)
One funny thing is, When a server error occurs, it does in fact display the Server Error view I defined, however it also outputs a default error message as well saying that the Error page could not be found.
Here's how it looks like:
Do you have any advice how I could fix these two problems? I really like Pure.Kromes approach to implementing these error messages, but if there are better ways of achieving this don't hestitate to tell me.
Thanks!
**EDIT : **
I can directly navigate to my views through the ErrorController by accessing /Error/NotFound or Error/ServerError.
The views themselves only contain some text, no markup or anything.
As I said, it actually works in some way, just not the way I intended it to work. There seems to be an issue with the redirect in the web.config, but I haven't been able to figure it out.
there is one more issue with that setup, when you have more complex routes and have several segments ex.
http://localhost:2902/dsad/dadasdmasda/ddadad/dadads/ddadad/dadadadad/
I got server error ->
Sorry, an error occurred while processing your request.
Exception: An error occured while trying to Render the custom error view which you provided, for this HttpStatusCode. ViewPath: ~/Views/Error/NotFound.cshtml; Message: The RouteData must contain an item named 'controller' with a non-empty string value.
Source:
my solution for that was to add additional route at the end after default route
routes.MapRoute(
"Default Catch all 404",
"{controller}/{action}/{*catchall}",
new { controller = "Error", action = "NotFound" }
);
hope it could help someone:-)
I got it to work. It seems my understanding of the problem was somewhat wrong to begin with.
In the web.config, I changed the following:
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Views/Error/ServerError.cshtml">
<error statusCode="404" redirect="~/Views/Error/NotFound.cshtml" />
</customErrors>
...
and
...
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/NotFound" responseMode="ExecuteURL" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" path="/ServerError" responseMode="ExecuteURL" />
</httpErrors>
This directly redirects to the views. My understanding was that I had to redirect to the error controller which in turn would redirect to the views, but apparently this was not the case. I'd like to thank you for your comments as they have made me analyze the problem again when I was already about to just ditch the custom error stuff and simply be lazy and display YSOD's. :)

Using IIS 7.5 Express to launch MVC 3 application, gives HTTP Error 404.20 - Not Found

I have an existing MVC3 application and I want to run that using IIS 7.5 express. When I try to do that I get the following error:
HTTP Error 404.20 - Not Found
No default document.
Most likely causes:
•A default document is not configured for the site.
•The URL contains a typographical error.
•Directory browsing is not enabled.
If I create a new MVC application and run that, it works fine. I compared the route maps and did not see any difference
For my application:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Program", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
}
For the sample application:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
The controller and action mentioned are available.
I also looked at applicationhost.config file and saw default documents as:
<defaultDocument enabled="true">
<files>
<add value="Default.htm" />
<add value="Default.asp" />
<add value="index.htm" />
<add value="index.html" />
<add value="iisstart.htm" />
<add value="default.aspx" />
</files>
</defaultDocument>
Any suggestions on how to fix this error?

ASP.NET MVC 3 HttpPost action method not found

I have a simple form that posts to an HttpPost action method, which returns its corresponding view. My issue is that I'm getting a 404 Not Found error. Oddly enough, if I change the form method and the attribute on the action method to a GET, then it works and the TestMethod view is displayed.
It seems I'm missing something for using POST, but my posts in other controllers work fine (e.g. account login and registration). Note the AllowAnonymous attribute is a custom attribute to be able to specify the controllers or actions that allow anonymous access, as opposed to specifying (via the Authorize attr) the controllers or actions that require authorization. I guess nothing is impossible, but I don't think that has anything to do with my issue. Any thoughts on what is wrong?
FORM:
#using (Html.BeginForm("TestMethod", "Test", FormMethod.Post, new { #id = "testForm" })) {
<fieldset>
<legend>Test Form</legend>
<input type="submit" value="Submit" />
</fieldset>
}
CONTROLLER ACTION:
[AllowAnonymous]
[HttpPost]
public ActionResult TestMethod() {
return View();
}
VIEW:
<h2>TestMethod</h2>
<p>HttpPost method was successful.</p>
REGISTER ROUTES METHOD FROM Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("favicon.ico");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// About
routes.MapRoute(
"About", // Route name
"about", // URL with parameters
new { controller = "Home", action = "About" } // Parameter defaults
);
// Faq
routes.MapRoute(
"Faq", // Route name
"faq", // URL with parameters
new { controller = "Home", action = "Faq" } // Parameter defaults
);
// Glossary
routes.MapRoute(
"Glossary", // Route name
"glossary", // URL with parameters
new { controller = "Home", action = "Glossary" } // Parameter defaults
);
// Register
routes.MapRoute(
"Register", // Route name
"register", // URL with parameters
new { controller = "Account", action = "Register" } // Parameter defaults
);
// LogIn
routes.MapRoute(
"LogIn", // Route name
"login/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"LogOn", // Route name
"logon/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);
// Default
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
AUTHORIZE ATTRIBUTE CODE:
// AllowAnonymousAttribute class
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class AllowAnonymousAttribute : Attribute { }
// GlobalAuthorize class
public sealed class GlobalAuthorize : AuthorizeAttribute {
public override void OnAuthorization(AuthorizationContext filterContext) {
bool skipAuthorization =
filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
if (!skipAuthorization) base.OnAuthorization(filterContext);
}
}
// RedirectAuthorizeAttribute class
public class RedirectAuthorizeAttribute : AuthorizeAttribute {
public string RedirectUrl { get; set; }
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) {
filterContext.Result = new RedirectResult(RedirectUrl);
}
}
GLOBAL FILTERS:
public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
filters.Add(new RequireHttpsAttribute());
filters.Add(new GlobalAuthorize());
filters.Add(new HandleErrorAttribute());
}
ROUTE REWRITING RULES:
<rewrite>
<rules>
<!-- Block all requests made to a website that do not have the host header set. -->
<rule name="Fail bad requests" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
</conditions>
<action type="AbortRequest" />
</rule>
<!-- Remove trailing slash from all incoming requests. -->
<rule name="Remove trailing slash" stopProcessing="false">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
<!-- Convert all requests to all lowercase. -->
<rule name="Convert to lowercase" stopProcessing="false">
<match url=".*[A-Z].*" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{R:0}}" redirectType="Permanent" />
</rule>
<!-- Any URL with (HTTPS == OFF) and (HTTP_HOST with colon) -> use for development testing. -->
<rule name="Development redirect to HTTPS" enabled="true" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
</conditions>
<action type="Redirect" url="https://{C:1}:44300{URL}" />
</rule>
<!-- Redirect any HTTP request to HTTPS. -->
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
I think I finally found the culprit. First of all, I'll concede that, knowing now what the issue was, the title of this question isn't very accurate. The problem mostly dealt with a rule rewrite in Web.config. Until responding to others' comments for this question, I had completely forgotten about the rule rewrites, which is why I hadn't checked them out further to begin with.
Anyway, the issue was a rule that rewrote URLs to all lowercase. I knew my account registration and login forms were working fine, so I checked them out and noticed that their Html.BeginForm statements were parameterless, which, evidently results in a lowercase url being generated. I tried parameterless POST request for my test method, and it worked. Then, I tried using parameters for action and controller in the Html.BeginForm statement, but this time I entered them as lowercase strings: Html.BeginForm("testmethod", "test"...). Sure enough, it, too, worked just fine, and the page source showed the form action as lowercase.
To fix my problem, I just had to set a condition to not match POST requests: <add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" negate="true" />. Note that the issue was not the lowercase rule specifically, but rather that the POST request was being redirected. I found one blog that discusses the issue of POST redirects being converted to GETs and resulting in errors, which is exaclty what I was experiencing. It's a couple years old, but apparently it's still pertinent info.
At any rate, I'm now back up and running. Thanks to all who threw in their two cents.
P.S. As I'm closing browser tabs and concluding my search, I figured I'd link to this SO question, as it is definitely related to my issue.
I tested your code just now. I initially got redirected for not using HTTPS so I disabled this attribute, but after that your code worked.
Here's my logical deducting...
If HTTPS was the problem, you wouldn't be getting a 404.
If you weren't logged in, you would be redirected to the login page.
The only thing I can think of is that your controller is either not named "TestController" or that your controller is in an area and you forget to supply this area with the BeginForm. Is it one of these by any chance?

MVC 3 - FormsAuthentication - Can't give access to my Login action

I'm using MVC 3 and FormsAuthentication:
<authentication mode="Forms">
<forms loginUrl="~/Account/" timeout="2880" />
</authentication>
My Account/Index page displays a login page, then using Jquery, it does a post to Account/Login to authenticate the user. Here's my AccountController action which handles it:
[HttpPost]
[Authorize(Users = "*")]
public ActionResult Login(string userName, string password) {
...
}
And the Jquery which posts to it:
$.post("/account/login", {
"userName": $("#userName").val(),
"password": $("#password").val()
}, function (data) {
...});
All the above works, but now I want to restrict general access to the site, so I added this to my root web.config:
<authorization>
<deny users="?" />
</authorization>
And in an attempt to allow access to the Account controller, I added this:
<location path="~/Account">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
Here's my problem. I can view the login page (Account/Index), but I can't submit to Account/Login. When I watch what's going on through Fiddler, I get this text returned from the server when posting to that page:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to here.</h2>
</body></html>
It seems that when I add "~/Account" to the full authorized user list it isn't really working like it would in a non-mvc project. Is there a special way to do this in MVC 3?
The path to a controller is typically not referenced as a relative path. You should just be able to say location="Account", e.g.
<location path="Account">
Can't be sure that's your issue as I can't test it right now, but it could be...
Do not use the AuthorizeAttribute at all for the Login action. It makes no sense to require authorization for the authorization process itself.
To require authorization for all your Controllers you could create
[Authorize]
public class AuthorizeController : Controller
{
}
and extend all your Controllers from this one except the AccountController.

Resources