MVC3 with razor web.config authorization for default route - asp.net-mvc

I am new to MVC and my question is this how can I setup my sites root to point to a specific Controller + Action and then in the Web.config file set the location + path of the root of the site, so eg: http://localhost:8080/ to be able to be accessed by all anonymous and logged in.
I have been playing with the location and path but just cant figure it out and in my Global.asax, I am not sure I have the right root to Home + Index as controller + Action.
Here is some code:
web.config (snippets)
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880"/>
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
I am not sure what the <location path=""> of the root of the site should be.
Global.asax (snippets)
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 }
);
}
The default route in Global.asax for the site I would like it to go to the Home Controller and the Action Index, so when you type http://localhost:8080/
Thanks in advance.

You are already doing it in the third parameter of MapRoute.
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
These specify your default controler and action, exactly as you want it.

Related

Redirect url in route config in Asp.net Mvc

How to Redirect url in route config in Asp.net Mvc...
My controller is "Home" and action is "Categories"
My url is localhost:49606/Home/Categories
now i changed using routes below
routes.MapRoute(
name: "Categories",
url: "Categories",
defaults: new { controller = "Home",
action = "Categories",
id = UrlParameter.Optional }
);
New url is localhost:49606/Categories
But still localhost:49606/Home/Categories url is avilabe...
I want to redirect localhost:49606/Home/Categories to localhost:49606/Categories.
or
Hide url localhost:49606/Home/Categories.
Please suggest me good way to redirect url or hide?
I want to redirect url in route config file in asp.net mvc 5
You can use Attribute routing.
In case of using attribute routing,you need to allow attribute routing to map your routes by adding following line in RouteConfig.cs
routes.MapMvcAttributeRoutes();
And then you can specify your route what you want.
no matter what your action name is.
[Route("Categories")]
public ActionResult GetCategories()
{
return View();
}

how to add multiple Url in MVC5 route.config

I am new to ASP.NET MVC5, I found one default route in routeconfig.cs file.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
so, If I am trying to access localhost:44300, I am redirecting to Home/Index action method.
So I am added Login page in Home/Index view page. after user successful login I am redirecting the user to Home/Details Method then In browser I am getting url as https://localhost:44300/Home/Details.
Here my question Is there any possibility to hide Home/Details like Home/Index. I am tried with adding another default route but it is failed.
Try adding following route before your default route in RouteConfig.cs file -
routes.MapRoute(name: "Details",
url: "Home/Index/{id}",
defaults: new { controller = "Home", action = "Details", id = UrlParameter.Optional }
);
This will convert - Home/Details to Home/Index
It is impossible to have the same url (http://localhost:44300) be mapped to 2 different controller actions. There can only be one default route.

How to prevent showing "Index" in mvc url

I use ActionLink in my MVC4 website as follows:
#Html.ActionLink("home", result: MVC.Home.Index())
after redirect, the url comes in the "www.mysite.com/home/index" form.
How can I prevent showing "home/index" or "index" in url??
You are seeing index in url because html.actionlink generates tag with href home/index. Instead of using helper html.actionlink use tag directly with href as home and have a default route which routes to index. But in this case you might loose the ability to route correctly when deployed in virtual directory.
You have to supply defaults to the corresponding route in your route configuration like so:
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 }
);
}

Filename as last argument to MVC controller

I have an MVC controller with a single method which contains a few arguments, the last of which is a filename with an extension and it is this last argument which causes issues.
Say the format I wish to create is:
http://example.com/worker/90bef68f718a434bb588120e717fa29c/foo.txt
This URL will 404, however if I remove the period or add a trailing / then the handler is hit and executed normally.
Unfortunately the URL format I am trying to implement cannot accommodate either of these work arounds.
The route is defined as follows:
routes.MapRoute(
name: "Worker Handler",
url: "worker/{guid}/{fileName}",
defaults: new { controller = "Worker", action = "Index" }
);
Some reading suggested changing the url template to include {fileName} to {*fileName}, however that too did nothing to fix this.
I can suggest two solution.
First Use route like this ( FileName and FileExtention in separate argument)
routes.MapRoute(
name: "Worker Handler",
url: "worker/{guid}/{fileName}/{fileextention}",
defaults: new { controller = "Worker", action = "Index" }
);
Another solution is to add following thing in web.config.
<system.webServer>
<modules>
<remove name="UrlRoutingModule-4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
</modules>
</system.webServer>
Get Help from http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html
Route should be like this.
routes.MapRoute(
name: "Worker Handler",
url: "worker/{guid}/{fileName}",
defaults: new { controller = "Worker", action = "Index" }
);
Hope this help you.

MVC MapRoute with username

I'm building a small MVC application. After a user logs in I want his/her route to display:
www.appname.com/username/
Underneath of course same action is called for each user e.g. /home/index. How do I write my MapRoute to achieve that and what other code(attributes) should I use?
Add this rout to your routes in global.asax.cs file
routes.MapRoute(
"RouteName", // Route name
"FixedUrlSegment/{UserName}/{Controller}/{action}/{id}/", // URL with parameters
new { controller = "ControllerName",
action = "ActionName",
id=UrlParameter.Optional
},
);
I think you should use a fixed segment as start-up point for your route to distinguish it from default or other routes
of course in log in action method you must redirect to that new route
return RedirectToRoutePermanent("RouteName", new { username = "UserName",
action = "Index",
controller = "Home",
id="userId"
}
);
// remember id is not required for that route as mentioned in global file
this example will redirect your page to url
www.appname.com/FixedUrlSegment/loggedusername/home/index/loggeduserid

Resources