Asp.net MVC Route Mapping - asp.net-mvc

I have view names like Folder-One/Page-One.aspx I want to do a base controller implimentation that all request go to one Base Controller, that returns the view based on the context. Obviously still keeping the .aspx in the path
I have folders like getting-started/application-faq.aspx but what I want to do is I want to create 1 controller that does all the return views, as the pages are basicly static html
Is this possible?

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{view}.aspx", // URL with parameters
new { controller = "Base", action = "ChooseView" ,view ="Page-One"}
);
and your action can choose view to show :
publict ActionResult ChooseView (string viewName)
{
return View("~/Views/"+viewName);
}

Related

Reusing controllers

Is it possible to re-use controllers in ASP MVC? And if so how?
Perhaps re-use isn't the right word. The situation is I have a menu and sub menu navigation bars as shown below (actually there is another nav bar what is shown)- I know the colour scheme needs some work
The upper bar is populated from a database, so there could be more or less than 3 plans.
The lower bar always has the same three entries. The views for each of these entries are the same regardless of which plan is selected, though they are different from each other. Obviously the data within them is different (populated from different tables).
That is Plan A -> Suggested Points view is the same as Plan B -> Suggested Points view.
But Plan A -> Suggested Points view is not same as Plan A -> Accepted Points view
In order to do this with the views I intend to use partial views, so the same view files can be re-used.
However, how can I do the equivalent for the controllers?
What I would like if for url paths such as:
/PlanA/SuggestedPoints
/PlanB/SuggestedPoints
To my mind I just want the Plan links to set a variable that tells the Points views which database they should hook up to. Which may be the wrong way to think of it and I suspect is incompatible with the url path suggestion above.
Suggested Approach
I would suggest it is better to include a controller name in the route, that way you won't get conflicts so easily with other controllers in your app.
You can modify your RouteConfig.cs file and map a new route. Make sure to add the custom route before the "Default" one.
Something like this:
routes.MapRoute(
"Plans",
"Plans/{planName}/{action}",
new { controller = "Plans", action = "Index" }
);
// Default route here.
Then you would have a controller called Plans with each of your actions having a parameter called planName that lets you identify with plan to work with...
public class PlansController : Controller
{
public ActionResult SuggestedPoints(string planName)
{
// create your view here, using the planName to get the correct data.
}
public ActionResult AcceptedPoints(string planName)
{
// create your view here, using the planName to get the correct data.
}
// etc.
}
This method will allow URL's in the following format:
/Plans/PlanA/SuggestedPoints, /Plans/PlanA/SuggestedPoints, /Plans/PlanB/AcceptedPoints, /Plans/PlanB/AcceptedPoints, etc.
NOTE: If your plans are in your database, it may be more beneficial to use an ID for the plan, but the URL's would look less friendly so that is up to you.
Finally, when you want to create your links in your view, you can use the following:
#Html.RouteLink("link text", "SuggestedPoints", new { controller = "Plans", planName = "PlanA" })
Your Exact Request
If you absolutely must use the URL formats you suggested, then you can do the following which requires a route for each action, but be wary that you will need to rely on the uniqueness of the Action names to ensure they don't conflict with other controllers...
routes.MapRoute(
"SuggestedPoints",
"{planName}/SuggestedPoints",
new { controller = "Plans", action = "SuggestedPoints" }
);
routes.MapRoute(
"AcceptedPoints",
"{planName}/AcceptedPoints",
new { controller = "Plans", action = "AcceptedPoints" }
);
routes.MapRoute(
"RejectedPoints",
"{planName}/RejectedPoints",
new { controller = "Plans", action = "RejectedPoints" }
);
// Default route here.
In this instance, the controller will remain the same as my first suggestion above. Which will allows URL's like as follows:
/PlanA/SuggestedPoints, /PlanA/SuggestedPoints, /PlanB/AcceptedPoints, /PlanB/AcceptedPoints, etc.
It can be something like this:
public class PlanController
{
public ActionResult SuggestedPoints(string plan) //or int planID
{
return View();
}
public ActionResult AcceptedPoints(string plan) //or int planID
{
return View();
}
public ActionResult RejectedPoints(string plan) //or int planID
{
return View();
}
}
and example urls next:
/Plan/SuggestedPoints/PlanA
/Plan/AcceptedPoints/PlanB

MVC creating 'folders' and sub-folders

In ASP.NET WebForms (well, HTML to be honest) we could reference pages within folders. EG, my structure could be (in regards to folders only)
root -> MyProductsFolder -> Shoes -> Ladies
And my website would show
www.mysite.com/MyProducts/Shoes/Ladies/Page.aspx
In MVC, we use a controller and it would appear that we can only ever have 1 level (folder) deep.
Is this right?
Without using URL rewriting, is it possible to have
www.mysite.com/MyProducts/Shoes/Ladies/Page
I assume the only way to do this is in the controller, but I can't create a controller named Shoes/Ladies
You can use MVC routing to created this URL. Your routing table is usually found in your AppStart > RouteConfig.cs class. You can use the route table to create URL maps to your actions in your controllers.
Assuming that MyProducts is your controller, and Shoes, Ladies are variables you want to accept you can do something like:
routes.MapRoute("MyProducts",
"MyProducts/{category}/{subcategory}/Page",
new { controller = "MyProducts", action = "Index" });
Note that your routes should be in order of most to least specific, so add this route above the default route.
When you navigate to /MyProducts/Shoes/Ladies/Page, it will map to your index action result in your MyProducts controller, passing variables for category and subcategory, so your controller will look something like
public class MyProducts : Controller
{
public ActionResult Index(string category, string subcategory)
{
//Do something with your variables here.
return View();
}
}
If my presumption is wrong, you want a view returned just for that URL, your route will look like:
routes.MapRoute("MyProducts", "MyProducts/Shoes/Ladies/Page", new { controller = "MyProducts", action = "LadiesShoes" });
And your Controller:
public class MyProducts : Controller
{
public ActionResult LadiesShoes()
{
//Do something with your variables here.
return View();
}
}
You can safely omit the final "/page" on the URL if you want to.
If I haven't covered your exact scenario with the above examples, let me know and I will extend my answer.
UPDATE
You can still put your views in a folder structure under the views folder if you want - and then reference the view file location in the controller - in the following example, place your view file called Index.cshtml in Views/Shoes/Ladies/ folder:
public class MyProducts : Controller
{
public ActionResult LadiesShoes()
{
//Do something with your variables here.
return View("~/Views/Shoes/Ladies/Index.cshtml");
}
public ActionResult MensShoes()
{
//Do something with your variables here.
return View("~/Views/Shoes/Mens/Index.cshtml");
}
}
You can use Attribute Routing to define the url of each action like below.
public class ShoeController : Controller
{
// eg: /nike/shoes/lady
[Route("{productName}/shoes/{xxx}")]
public ActionResult View(string productName, string xxx)
{
}
}
Routing Attribute offers flexibility and better code organization. You can check the route definition in the same spot.

How can I pass the URL to the controller while routing ignores it?

Is there a way within asp.net MVC 2 whereby I can route a request and have a portion of the URL ignored and passed to the controller as a variable?
My needs state that I must store pages dynamically in a database, and they should be accessible by looking at the URL and reading the URL segments to find the relevant page. Effectively, I need a Site controller, to which the remaining portion of the URL will be passed.
Site-Controller/this/is/a/page
So this in case the site controller would pick up the /this/is/a/page 'string'
Is this possible?
Thanks!
Yes, use a wildcard route, like:
routes.MapRoute(
"SiteController", // Route name
"Site-Controller/{*url}", // URL with parameters
new { controller = "SiteController", action = "Index" }, // Parameter defaults
null // constraints
);
Then your action looks like:
public ActionResult Index(string url)
{
}
Create a wildcard Route in Global.asax which captures everything after the first segment of the url and passes it to your Action method:
routes.MapRoute("Page",
"Site-Controller/{*urlsegments}",
new {
controller = "Site-Controller",
action = "YourAction",
urlsegments = ""
});
Make sure your Action method accepts a 'urlsegments' parameter and you can work with it from there:
public ActionResult YourAction(string urlsegments)
{
// Do something with the segments here
}

ASP.NET MVC Map String Url To A Route Value Object

I am creating a modular ASP.NET MVC application using areas. In short, I have created a greedy route that captures all routes beginning with {application}/{*catchAll}.
Here is the action:
// get /application/index
public ActionResult Index(string application, object catchAll)
{
// forward to partial request to return partial view
ViewData["partialRequest"] = new PartialRequest(catchAll);
// this gets called in the view page and uses a partial request class to return a partial view
}
Example:
The Url "/Application/Accounts/LogOn" will then cause the Index action to pass "/Accounts/LogOn" into the PartialRequest, but as a string value.
// partial request constructor
public PartialRequest(object routeValues)
{
RouteValueDictionary = new RouteValueDictionary(routeValues);
}
In this case, the route value dictionary will not return any values for the routeData, whereas if I specify a route in the Index Action:
ViewData["partialRequest"] = new PartialRequest(new { controller = "accounts", action = "logon" });
It works, and the routeData values contains a "controller" key and an "action" key; whereas before, the keys are empty, and therefore the rest of the class wont work.
So my question is, how can I convert the "/Accounts/LogOn" in the catchAll to "new { controller = "accounts", action = "logon" }"??
If this is not clear, I will explain more! :)
Matt
This is the "closest" I have got, but it obviously wont work for complex routes:
// split values into array
var routeParts = catchAll.ToString().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
// feels like a hack
catchAll = new
{
controller = routeParts[0],
action = routeParts[1]
};
You need to know what part is what in the catchAll parameter. Then you need to parse it yourself (like you are doing in your example or use a regexp). There is no way for the framework to know what part is the controller name and what is the action name and so on, as you haven't specified that in your route.
Why do you want to do something like this? There is probably a better way.

ASP.NET MVC view locations and routing

I have a base controller that I use to return basic views like this.
public ActionResult Index(string pageName)
{
return View(pageName);
}
public ActionResult LanguageSpecific(string ul, string pageName)
{
var result = View("sv/" + pageName);
return View(result.ViewName);
}
The controller's name is home is there a way that for it not to look for the sv content in /home but just in /sv
"EnglishRoute", // Route name
"{pageName}.aspx", // URL with parameters
new { controller = "Home", action = "Index", pageName = "" } // Parameter defaults
);
routes.MapRoute(
"SwedishRoute", // Route name
"{ul}/{pageName}.aspx", // URL with parameters
new { controller = "Home", action = "LanguageSpecific", ul = "",pageName = "" } // Parameter defaults
);
It looks in these locations:
~/Views/Home/sv/index.aspx
~/Views/Home/sv/index.ascx
When you call the View method you can pass in an app-relative path that starts with "~/" and then ASP.NET MVC will use the exact path you specify:
return View("~/UseExactlyThisFile.aspx");
That way it won't do its search in the various paths and locations that are pre-configured.
Please keep in mind that this doesn't have very much to do with routing (though it does a little bit).
If you try to localize your pages, why don't you use resources? With the pattern above you don't really take the advantages of mvc. Or do i misunderstand you? A simple solution would be to use an action filter which picks up the language identifier from the route and sets the UICulture. The Views then may use resources to localize their content.

Resources