I have to deal with a legacy asp.net mvc app, which is not configured as I am used to. After a normal logout via:
FormsAuthentication.SignOut();
and
return RedirectToAction("Index", "Home");
the URI contains:
ReturnUrl=%2f
This is not usually the case. How can I suppress this?
Alternatively, when I try to access a page that requires authentication/authorization the login page appears but no appropriate ReturnUrl= is generated (i.e. the URI stays as it is).
Is this an IIS issue, which I have read somewhere, or is the asp.net FormsAuthenticationModule not properly configured? Thanks.
ReturnUrl is added during an unauthorized redirect. Someone appears to be redirecting the root url (/) to itself or to Home/Index. %2f is the encoded form of "/".
I would check the authorization section of the web.config for something that looks wrong.
This is depends on you routing, right? If you have this as you last route configuration:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
If nothing works then add authentication mode="Windows" in your system.web attribute in your Web.Config file. hope it will work for you.
Related
I have an ASP.NET Core 3.1 MVC router question.
When I change index route to [Route("Index")] in HomeController enter image description here, and then enter the url localhost:5000/index in web browser, it works!
But in my ASP.NET Core MVC project, the default startup url is localhost:5000/home/index enter image description here.
Question
How can I change the default startup url to localhost:5000/index?
I wish use localhost:5000/home/login to visit the login function in the HomeController.
Add following route before your default route.
endpoints.MapControllerRoute(
name: "customindex",
pattern: "index",
defaults: new { controller="Home", action="Index"});
});
pattern can support default value but it works with common scenario like controller/action but if you want specific pattern then you have to specify pattern and also set defaults so those will supply as route value in order to identify action to call.
As we know that routing follows the routes provided in RegisterRoutes function in RouteConfig.cs which defines the controller and action in default routes.
My project only have default route like:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
So how does routing detects that whether we are accessing some action or pointing to a file like image, css or js.
The short answer is that any request for a resource that has a . in its name is directly served by IIS without invoking MVC routing. Conversely, requests that do not contain a . are passed to MVC for routing. Importantly this is just default behaviour.
As a quick test, fabricate a url for any resource that does not exist in your project such as \asdf and note the 404 error generated by MVC. Now repeat the test with \asdf.asdf and note the 404 error is generated by IIS.
It is possible to change this behaviour with directives in web.config. For example, see How do I route images using ASP.Net MVC routing?
I moved a solution from one machine to another and am having nightmares. I've got one problem left. The default page can't get served, it says Resource cannot be found. I have in the routeConfig.cs
routes.MapRoute("Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
I get Resource cannot be found.
I can access the default page by :-
http://example.com/home/index
The other questions don't seem to answer it.
EDIT: If I put the above line of code at the top of the Routing code, the default page works but other pages don't. If I put the code at the bottom, other pages work but the default page doesn't.
Try look in properties there is "Web" tab were you cat chose server and project URL, hope
If you have only default routing on RouteConfig file in development machine and keep getting 404, it seems that your problem comes from project's virtual path settings.
http://forums.asp.net/t/1893154.aspx?The+resource+cannot+be+found+404
If application is deployed on local host (or you have set Use local IIS
Web Server) then it means you have created a virtual directory named
"YourSite".
Right click your app project on VS, choose Properties, then go to "Web" part and set your site's virtual path on "Project Url".
For virtual directory problem on live IIS server, see here to add virtual directory into destination machine before deploying your app.
In addition, if you have custom routing rule, make sure the custom route placed properly on top of default routing rule.
The problem was that I had two Config Rules pointing to the same address. One of the other rules was pointing to the wrong controller. The wrong controller was erroring because I had set up the action, I had used the name of the controller as the action, thats a no-no. The above rule was correct but the incorrect rule was
routes.MapRoute(
name: "pagelist",
url: "pagelist",
defaults: new { controller = "pagelist", action = "pagelist", id = UrlParameter.Optional }
);
the action should have been Index.
I have created a folder admin inside controller folder and wrote few controllers in it. I want to access all functions in that controllers in that folder by a url like abc.com/admin//.
I can get it working directly when its put directly in controller folder.
EDIT
WHAT HAVE YOU TRIED?
Just created a folder and wrote normal controllers in it. But I don't know how to route it.
Well, i would suggest, rather than just creating a folder and create controllers inside of it. You should create an Area
When you create an Area in asp.net mvc, it will automatically create folders(controller, model etc) for you. And the routing setup as well.
For more info, please visit
http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm
Add a route for the admin controllers in your route config if you want to support that path in the URL. Something like:
routes.MapRoute(
name: "Admin",
url: "admin/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But #DotNetDreamer is right that Areas are a better solution for the admin functionality of your site.
Sometimes when I launch my MVC 3 project it attempts to load the fully qualified URL for the view being rendered instead of the action within the controller (Which gives me a 404 error). Other times it works fine and actually hits the controller action like it's supposed to, but it's about 50/50.
The URL it hits sometimes is: http://localhost:xxxx/Views/Account/LogOn.cshtml
Here is the default route setup in the Global.asax file:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }
);
I also tried removing the /{id} parameter from the route as I don't feel it's needed for the logon screen.
Any ideas? Currently the project is setup pretty simply with the default action method LogOn in the AccountController etc. The only thing I did was change the controller and action in the global.asax file.
Try this :go to Project Properties > Web > Start Action
And check the Specific Page option - leaving the text box blank.
You are probably using Visual Studio and you probably are actively editing a .cshtml page when you hit debug.
Try launching the debugger when you are either looking at a code file or a file from a project that isn't in the startup project (ie, your EF/model project) and see if that launches the debugger to the correct URL.
There might be a setting in the project properties that specifies the startup URL. I'll look for it and edit this post if I find it.
I'm guessing you using cassini (builtin dev web server in VS.Net)? If so I get this all the time and seams to be a bug in VS.Net. Switch to IIS 7.5 and you don't get it any more