I have a url like www.abc.com in mvc3 application, i set a default map.route like this
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "UserLogOnPage", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);
now what is happening when i type www.abc.com and enter it goes to Controller:UserLogOnPage Method:Logon and in browser url is also showing www.abc.com which is correct, but when enter url like www.abc.com/UserLogOnPage/Logon same method and controller ,i want url like www.abc.com not www.abc.com/UserLogOnPage/Logon how can i do this.
Thanks
I am almost certain this isn't what you are actually trying to do - that route configuration will result in any URL being redirected to the LogOn page.
Going by the url what it looks you are after is actually authentication i.e. if an anonymous user visits www.abc.com they are asked to log on before they can get access to the site.
Luckily for you, MVC has already done the hardwork all you need to do is decide how you want it to work. Authentication can be applied at various levels and the scope is determined by where via the AuthorizeAttribute is set.
Global.asax - authentication required by every action
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
...
filters.Add(new AuthorizeAttribute());
}
Controller - authentication required for all actions within a particular controller
[Authorize]
public class AccountController : Controller
{
...
}
Action - authentication required for this action only
[Authorize]
public ActionResult AccountDetails()
{
...
}
To determine which page you want users to be redirected to as a result of the AuthorizeAttribute, you can configure the authorization section in the web.config
<authentication mode="Forms">
<forms loginUrl="~/UserLogOnPage/LogOn" />
</authentication>
Related
I have an MVC website that uses Forms Authentication.
I am attempting to add a default landing page to my site ( landing.html ) that each user hitting
http://mywebsite.com for example will be redirected to.
I tried setting the default document in the web.config, but I always get redirected to the Account/Login page.
I do not want to change the login url because that will make the user's whose session has expired to go to the landing page, which is not what I want.
Any help would be appreciated.
Thanks
Update your "default" route. MVC projects have the following route defined in the RouteConfig class and is called in the Global.asax during application startup.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Just replace "Home" and "Index" with the controller and method you want to use as your default landing page.
Also be sure that if the controller has the [Authorize] attribute added to it then you need to add the [AllowAnonymous] attribute to the method you want to use.
I have an MVC 3 app, my default route is app/index, which mean that if a user hits "http://www.something.com",
they are acutally seeing "http://www.something.com/app/".
However, I want to show the actual route always, which mean that when a user hits
"http://www.something.com", I want the url in address bar to be shown as "http://www.something.com/app/". How can I achieve this?
You can use an redirect in your action as follows:
public class HomeController : Controller
{
public ActionResult Index(){
return RedirectToAction("Index","App");
// or you can do a redirect to a URL. like 301.
return RedirectPermanent("/app");
}
}
I'm getting a 302 error returning on a single CSS file on an ASP.NET MVC 2 site in localhost this morning and I don't know what would have changed to cause this.
The localhost site uses IIS 7.5, though I've had limited experience with IIS so I haven't looked to much in to what could be going on there.
The URL to the CSS file is:
http://localhost/MySite/Content/Site.css?v=16
and the location header on the response looks like this:
/MySite/Account/Login?ReturnUrl=%MySite%2fContent%2fSite.css%3fv%3d16&v=16
This makes me think that MVC is redirecting the static file or something like that, however if that was the case, then I would expect all my images, CSS and JavaScript files to be doing the same which they're not. Just in case, here is a simplified version of RegisterRoutes() in Global.ascx:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("", "Account/{action}/", new { controller = "Account" });
routes.MapRoute("", "{action}", new { controller = "Home", action = "Index" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Error",
"{*url}",
new { controller = "Home", action = "ResourceNotFound" }
);
}
Also, if I change the name of my CSS file to Site2.css and reference that instead, the same thing happens.
What's going on?
The redirect to the logon method makes it look like this is because of permissions on the directory or the file rather than an MVC route catching it. (If it were caught by an MVC route, it would probably rather result in an error determining which controller and/or action to use.)
ASP.NET MVC itself leaves static files alone, but if ASP.NET in general decides that the anonymous user doesn't have access to the CSS file or its directory, ASP.NET will redirect to the log-on URL, which will be an ASP.NET MVC action.
Looks like the authorization rules in the web.config are saying that you have to be authenticated to see the css pages. You should be able to prove that by logging in and seeing if you can get the css file to be served correctly.
I'd add a location section to the web.config to remove the authorization requirement on the content directory. Taken from http://support.microsoft.com/kb/316871
<!-- This section gives the unauthenticated user access to all of the files that are stored in the Content folder. -->
<location path="content">
<system.web>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
</location>
Is any special routing or IIS config needed when a controller action uses the same URL as a virtual directory?
I have an ASP.NET MVC 1.0 application that needs Windows Authentication applied to a single action ("/Login/FromWindows"). To do this, we've setup a virtual directory with the same path as the action (e.g. "/Login/FromWindows") and enabled Windows Authentication on it in IIS.
When I visit the /Login/FromWindows URL, I get an empty HTTP 200 response and nothing is logged in the server text log. The "FromWindows" action should be logging messages and redirect the user to the home page.
It seems like the action code is simply not being executed, so there is possibly a conflict with the virtual directory.
Route config in Global.asax.cs
public static void RegisterRoutes(RouteCollection routes)
{
// snipped: ignored routes for images, scripts, etc.
routes.MapRoute( "Default", "{controller}/{action}",
new { controller = "Home", action = "Index" } );
}
You are right, the action code isn't being executed. That's because existing file paths (virtual or not) take precedence over MVC routing rules.
Why are you using a virtual directory? Just set authentication to windows in the web.config and use the [authorize] attribute over the corresponding action methods.
Web.config:
<configuration>
<system.web>
<authentication mode=”Windows” />
</system.web>
</configuration>
Action Method:
[Authorize]
public ActionResult SomeAction()
{
return View();
}
Visit http://www.asp.net/mvc/tutorials/authenticating-users-with-windows-authentication-vb for more information on mvc with windows authentication.
Just simple is using [Authorize] attribute a Chevex mention above, or if you want more, you can customize the Authorize by extension it for your business. IMHO.
I have deployed my application to a server running IIS6 using the method which invloves changing the routes to:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
And adding a handler in IIS for .mvc extentions. This is working fine for the most part until I add the [Authorize] attribute to HomeController class.
This ends up in the app trying to redirect the user to the logon page which is what I expect however the logon page URL is shown as http://server/virtualdir/Account/LogOn?ReturnUrl=%2fvirtualdir%2fDefault.aspx
This is causing a problem as no .mvc extension is being added to the Account controller part of the URL.
The problem has been solved by changing the following in web.config:
<authentication mode="Forms">
<forms loginUrl="~/Account.mvc/LogOn" timeout="2880" />
</authentication>
Not directly an answer to your question, but from my experience it worked just fine to deploy an application with the new routing features just as it is to IIS6 and add a wildcard mapping to aspnet_isapi.dll. Then you can use any URL you want, and nobody will notice when you change to a newer version in the future.
Yes, static file handling is theoretically less efficient this way, but you will need really a lot of traffic to notice anything. And if you really get a lot of traffic, you still could and even should move all your static files to a another domain/subdomain (or even a CDN) anyway, like stackoverflow.com does. It can still point to the same server, you just use different IIS settings for this subdomain site. But with e. g. just a few thousand visitors per day you don't even have to think about it.