I'm a kind of newbie with routing, I want to give the possibility to my user to go to the admin part of the site by just entering www.hisSite.com/admin whith a route that will redirect to the SiteAdmin controller and Index action. Is it possible?
I probably didn' understand something but I'm baddly stuck....
There are a few options that you can choose such as redirecting or creating a separate Area for the admin side however I think since you're a newbie just create a controller called AdminController and include an action called Index like below:
public class AdminController : Controller
{
public Action Index()
{
// Some action
}
}
To call this you'd only need to enter:
www.hisSite.com/admin
or
www.hisSite.com/admin/index
What's happening here is by using a convention of adding Controller suffix to each controller class, routing will recognise the first part i.e. Admin as a route when requested by a browser.
routes.MapRoute(
name: "Admin",
url: "admin/{action}/{id}",
defaults: new { controller = "SiteAdmin", action = "Index", id = UrlParameter.Optional }
This will route all urls pointing to www.hisSite.com/admin to actions and views in the SiteAdmin controller.
A simpler way is to just create a Admin controller ofcourse.
Related
I have an application where I don't want to use any "folders". EG
http://mydomain/Index ,
http://mydomain/Edit ,
http://mydomain/Admin ,
and so on....
I modified my defauly RouteConfig initially to look like this:
routes.MapRoute(
name: "maproute1",
url: "{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
I have a Views/Home folder with all my views.
It works like a charm so whenever I enter the actionname, I don't have any issues.
If, hypothetically, I wanted to KEEP this url structure the same where I ONLY show the root domain / action-page... and I want to have SOME of these actions use one controller (HomeController.cs) and other actions (e.g. Admin) use another contoller (AdminController), is there I way I can modify my routes to say... ok.. if the action is "Admin" then use the AdminController? I have the AdminController set up with the action for "Admin" defined. I tried changing my routeconfig like this:
routes.MapRoute(
name: "maproute1",
url: "{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "maproute2",
url: "{action}/{id}",
defaults: new { controller = "Admin", id = UrlParameter.Optional }
);
and then I set up a Views/Admin folder for the Admin view (and some other actions/views I want)
But it doesn't work. IF I have an "Admin" action in the home controller, it fires from the home controller. But I want the action to fire from the AdminController. Keep in mind I'm not just going to use ActionLinks to navigate to these pages so I can't just use an Actionlink and specify the controller. Some people are going to be going to links via a direct URL/bookmark.
If I wanted to keep my URL structure like this (http://mydomain/Action) I could just slap all my actions into ONE single controller and it would work, but I like to keep my code neat so that one controller handles functionality related to one set of models and another controller related to another set of models. For now it seems like I have to either (a) have one MASSIVE controller that is just going to get bigger and bigger or (b) I HAVE to have a more detailed path in there to have something to tell MVC what controller to use.
(BTW, I can't use querystrings... long story... don't ask)
MVC was designed to give people more control over layout from what I understand. I've been a web forms programmer for 13 years, but so far it seems that MVC has done this at the expense of giving you less control in other areas.
You may need to hardcode your Action names in your routes.
routes.MapRoute(
name: "maproute1",
url: "Home/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "maproute2",
url: "Admin/{id}",
defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
http://localhost/Admin should take you to the Admin/Index view and http://localhost/Home should go to Home/Index view
one simple workaround is to create an admn action in home controller and then redirect from there to Admin controller but i am not sure you want to do ths as it may hit performence
public ActionResult Admin()
{
return Redirect("Admin/Index");
}
i would say try something other if you can and even then if you cant find a way out use this workaround
I have kind of a mixed application with Angular and ASP.NET MVC.
It works this way:
User enters example.com, MVC controller checks if he is Authorized:
if he is, controller returns view Main which contains Angular app.
if he isn't, then a view Index is shown, with login/register form
Logging in/ registering is done within Account controller at example.com/Account
Angular app has html5Mode enabled, so inside-app routing is like example.com/home,example.com/profile etc.
I've tried messing aroung with MVC's RouteConfig, as well as simple rewriting
protected void Application_BeginRequest( Object sender, EventArgs e )
{
string url = Request.Url.LocalPath;
if ( !System.IO.File.Exists( Context.Server.MapPath( url ) ) )
Context.RewritePath( ROOT_DOCUMENT );
}
source: ui-router GitHub page
But nothing adresses my situtation.
Above code basically disables whole MVC routing, so I can't access /Account/LogIn
I've faced a similar issue. I think the best answer I can come up with is to define specialized routes that will allow access to the controllers, and then define a DefaultAll route that will default to the specific controller, and action method that has my Angular code.
For example in my case I have one controller Page, and my first route is
routes.MapRoute(
name: "Default",
url: "Page/{action}/{id}",
defaults: new { controller = "Page", action = "Main", id = UrlParameter.Optional }
);
This means any request with Page/ - will route appropriately to the controller, and use the action method defined in the request. For example Page/main, Page/one, Page/two
My second request is a catch all that defaults to the controller Page, action method Main.
routes.MapRoute(
name: "DefaultAll",
url: "{*.}",
defaults: new { controller = "Page", action = "Main", id = UrlParameter.Optional }
);
This means when I paste a url like http://localhost/myapp/persons , it will load the Page controller, Main action method, which has my Angular routes defined and then Angular will parse the persons part of the URL to perform its routing.
This does mean that if you have more controller, you will need a specialized route for each one.
If you have come up with a better way please do let me know.
I am quite new to MVC. I am facing a problem with routing right now. My project URL is /account/Create. I can access controller and do my stuff for Create, but
I need to access /account controller because I need to write code in that level.
/account/create - I can access the code this level
/account - dont know how to access this controller
Project Stucture:
Sample Project
Controler
Model
View
What am I supposed to change in the following code?
//global.asax.cs
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
);
}
/account/create is accessing code in the Account controller. Create has to be a method on the Account controller (unless you modify the default routes). Any public method you define on the account controller is accessible via /account/method URL. Based on the route you posted, going to /account URL is going to call the account controller Index method:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
**new { controller = "Home", action = "Index", id = UrlParameter.Optional }** // Parameter defaults
);
That action = "Index" part above is defining what the default method on the account controller is, so going to /account URL is equivalent in this case to /account/index URL
And I just noticed that you spelled account wrong in the question, not sure if that may be your issue ;)
Update
Not sure if this is what you're after, but if you need to write code at the /Account level you can do this in the constructor of the controller.
Unless you substantially customize MVC, then controllers correspond to classes derived from Controller in mvc, and actions correspond to methods on those controllers.
What are you trying to achieve when you say you can't access the controller /Account?
The controller is only a container for Actions so you need to specify an Action. Of course, you can have a default Action in case an action isn't specified. That is specified in default the route above. It's called Index
I have the following route set up:
context.MapRoute(
"MyAccount_default",
"MyAccount/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
and the following links:
/MyAccount/Access/Login
/MyAccount/Access/Logout
/MyAccount/Access/Register
Is there some way that I can create a routemap so that entering:
/Login
/Logout
/Register
Would direct the request to the MyAccount area, Access controller and Login/Logout or Register methods? If I have
the routemap then can it belong inside the routemap of the MyAccount area or does it need to be outside that?
You really should be specific with your routes. If you want something outside of the standard routes, add those to your route tables.
context.MapRoute(
"Login",
"/Login",
new { controller="Access", action = "Login"}
);
Add that before your default route.
The other option is to use our default route, but in addition use something like the AttributeRouting project at https://github.com/mccalltd/AttributeRouting
and specify your additional route on your action methods in each controller.
note that you could code on your Login action method something like:
[GET("Login")]
public ActionResult Login()
{
}
so your default routes would be used and in addition this extended route. I believe that should work anyways :)
I've created a website with ASP.NET MVC. I have a number of static pages that I am currently serving through a single controller called Home. This creates some rather ugly URLs.
example.com/Home/About
example.com/Home/ContactUs
example.com/Home/Features
You get the idea. I'd rather not have to create a controller for each one of these as the actions simply call the View with no model being passed in.
Is there a way to write a routing rule that will remove the controller from the URL? I'd like it to look like:
example.com/About
example.com/ContactUs
example.com/Features
If not, how is this situation normally handled? I imagine I'm not the first person to run in to this.
Here's what I've done previously, using a constraint to make sure the shortcuts don't conflict with other routing rules:
routes.MapRoute(
"HomeShortcuts",
"{action}",
new { controller = "Home", action = "Index" },
new { action = "Index|About|ContactUs|Features" }
);
Add defaults for the controller names in the new statement. You don't have to have {controller} in the url.