I have a site that has basic functionality, but can be overriden based on different clients and different partners. The Routing is set up to handle the client name and the partner name as part of the route:
routes.MapRoute(
"DefaultRoute", // Route name
"{client}/{portal}/{controller}/{action}/{id}", // URL with parameters
new { client="UNKNOWN", portal="UNKNOWN", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Enterprise.Portal.Controllers" }
);
I have a helper class to determine if a view exists that will supercede the normal view. The site has different clients and each client has different partners. These clients can provide HTML if they do not want the default views, and the partners can do the same. I keep these alternate views in a folder. The helper class takes the information and if an alternate view exists, returns the file path to this view. If it returns null or empty string, the normal view is used.
public static string ViewPath(string basePath, string client, string partner, string controller, string viewname)
// This returns something like C:\Sites\Portal\UI\ClientName\PartnerName\ControllerName\View.cshtml
In my controller, if this returns a non null or empty value, How do I provide that view to be used. Here is what I did, which doesn't work:
if (String.IsNullOrEmpty(this.model.CurrentViewLocation))
{
return View(model);
}
else
{
return View(this.model.CurrentViewLocation, model);
}
I am getting the following error, because obviously the return View() constructor can not use path names, only View names. Is there a way to accomplish this? I can convert the paths to Virtual Web Paths if needed like "~\UI\Client\Partner\Controller\View.cshtml".
Server Error in '/' Application
The view 'C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Account/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.aspx
~/Views/Account/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.ascx
~/Views/Shared/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.aspx
~/Views/Shared/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.ascx
~/Views/Account/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.cshtml
~/Views/Account/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.vbhtml
~/Views/Shared/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.cshtml
~/Views/Shared/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.vbhtml
I'm guessing a better way to do this would be to add the Client folder and the Partner folder to the Location Formats of the view engine which are used searched for the views. But the format string only includes {0} for the controller and {1} for the viewname. I would need to override it to pass the Client and the partner as well, which are both passed via the route.
I can convert the paths to Virtual Web Paths if needed like
"~\UI\Client\Partner\Controller\View.cshtml".
Yes, that's precisely what you should do because that's what the View method expects - a relative path to the root of the website:
return View("~/UI/Client/Partner/Controller/View.cshtml", someViewModel);
Related
I have developed an MVC5 application for one of our client. It works fine. Now we have more clients where all the functionalities are same, but the view is different for each client(Not only the layout, but the html structure itself is different in each view).
What I was doing to distinguish the clients is to provide different urls, adding a client identifier (because we need to identify the client even before login), and filtereing it in the RouteConfig as given below:
routes.MapRoute("ClientRoute", "{client}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id =
UrlParameter.Optional },
new RouteValueDictionary
{
{ "client", "icici|federal|pnb|sbi" }
});
where the icici,federal,pnb and sbi are the valid clients.
and I could use this below code to distinguish the clients for any client specific logic.
var clientName = HttpContext.Current.Request.RequestContext.RouteData.Values["client"].ToString();
What I want is to have separate View folders for each client
Views (Default, should be taken from here if not found in other locations)
ICICI_Views
SBI_Views
FEDERAL_Views
PNB_Views
....
these folders will have the layout and cshtml files.
Any action having return View() or return View("viewname") should pick the corresponding views from the respected client folders.
Please help me if anyone know any solution to implement this (like configuring RouteConfig or DisplayModeProvider class, etc). I dont want to have a if-else check in each return view statement and specify the full path.
Thanks in advance :)
You can specify the path of the view while returning from action method, For example if the client is ICICI then return View("~/ICICI_Views/Home/Index.cshtml"); and if no client found you can use return View();
return View("~/ICICI_Views/Home/Index.cshtml");
I'm new to ASP MVC programming and wanna ask about how the route is configured.
For example I have the Home Controller
public ActionResult Home(){
return View("Index")
}
This will find the Index.cshtml under /Views/Home/
However if I rename the Home Folder to Homees for example, the view is not found and also I try to return View with View("~/Views/Homees/Index.cshtml") this is not change that the controller not found the view.
Is this the default of the asp mvc? and it's possible to change this one?
There are few points.
ASP.net MVC is convention based. It is also specified by #Petar Minev. When it comes to search for view it use following method. It take controller name as directory name and view name file name with different extention ( like cshtml, vbhtml , aspx ) based on view engine. ( As you are using cshtml it seems that you are using both Razor and Webform view engine).
For search it will first go to directory with controller name and search for specified view. If it is not available there then it goes to shared folder.
Above is default behavior of ASP.net MVC.
Now you change folder name then first solution you have tried that must work as it works for me. ( Please check that your folder name is correct. Make sure you did not rename for area directory).
public ActionResult Home(){
return View("~/Views/Homees/Index.cshtml")
}
Another solution is to rename controller with HomeesController ( So it will automatically locate correct directory)
If you continue with this convention for other folder like the way you add "es" in "Home" it is better to add this convention in default search for view.
( You can do this by either inherit from default RazorViewEngine or change RazorViewEngine parameter)
For example
protected void Application_Start()
{
RazorViewEngine engine = (RazorViewEngine)ViewEngines.Engines[1];
List<string> currentFormats = engine.ViewLocationFormats.ToList();
currentFormats.Insert(0,"~/Views/{1}es/{0}.cshtml");
engine.ViewLocationFormats = currentFormats.ToArray();
... Other application start code
}
Razor View engine is default view engine for ASP.Net MVC. This Razor view engine is configured to locate path at specified path i.e. "~/Views/{1}/{0}.cshtml".
Here {1} placeholder specifies controller name and {0} represents view name.
Say, for Example any request for Index action in Home controller will look for view at "~/Views/Home/Index.cshtml".
Now if you want to change this default path then you have to define custom view engine. Here a sample example how can you define a custom view engine and change the default path.
public class MyCustomViewEngine : RazorViewEngine
{
public MyCustomViewEngine()
{
ViewLocationFormats = new string[] {
"~/MyViews/{1}/{0}.cshtml",
"~/MyViews/Shared/{0}.cshtml" };
MasterLocationFormats = new string[] {
"~/MyViews/{1}/{0}.cshtml",
"~/MyViews/Shared/{0}.cshtml"};
PartialViewLocationFormats = new string[] {
"~/MyViews/{1}/{0}.cshtml",
"~/MyViews/Shared/{0}.cshtml"};
FileExtensions = new string[] { "cshtml" };
}
}
You also need to register custom view engine with ASP.Net run time at Application _Start() event.
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MyCustomViewEngine());
Your controller code is seems wrong, if your controller name is Home then code will be like this with index action
public class HomeController : Controller
{
public ActionResult Index()
{
return View("~/Views/Homees/index.cshtml");
}
public ActionResult Contact()
{
return View();
}
}
you just used the controller name as action name, by default in view folder there is a separate folder for each controller like for Home controller there will be a folder named Home, and inside that there will be separate cshtml file for each action result, like for my code there is a two action result name Index and Contact so under Home folder there will be two separate cshtml for both as index.cshtml and contact.cshtml. So when we request index action it will go for index.cshtml and for Contact action contact.cshtml by default, but we can spacify our own view for any action like my index view, and it works fine, your approach was correct but only problem was the Controller name and action name I think, try this way it may help
by default Microsoft's ASP.NET MVC is created over one Folder Convention which means that all files that will be Controllers should be under Controller folder, each file which will be View should be under View folder,
also if you create Mvc Route for example MyProfile, in the MVC you'll get contorller with this name and folder under the view's.
All this is controlled by the default routing which is knowing where to find, Views and controllers, so if you want to make some changes or modifications you should go to ASP.NET web site and look some tutorials for MVC Routing
Hope i helped :)
Simple just go to App_Start and Open RouteConfig.cs File and change route controller "Home" to "Homees" by default it set as "Home". If you rename your HomeController to "HomeesController" you should change to rountconfig by default route. check below image
After that open "HomeesController" from Controller folder here you can add action for view
public ActionResult Index()
{
return View();
}
and Add Action to View "Homees" Folder
I have an architecture where I have numerous objects I can configure. Examples of URLs I want are as follows:
/configuration/building/add
/configuration/building/edit/1
/configuration/group/add
/configuration/group/edit/1
I have a Configuration controller but how do I intercept or deal with building/add and building/edit/1 etc... If it were AddBuilding I could simply add an AddBuilding() function, and similarily how do I get it to work for configuration/building/edit/
Here's what you can do for the first one - open up the Global.asax.cs file of your site and put this in RegisterRoutes before the standard MVC catch-all route (the one that uses the route "{controller}/{action}/{id}"):
routes.MapRoute("AddBuilding", "configuration/building/add",
new { controller = "Configuration", action = "AddBuilding" });
The others will be the same, but different names (first parameter) and action, whislt the edit routes but would include an {id} route placeholder and route parameter (but not optional - unlike the MVC default route):
routes.MapRoute("EditBuilding", "configuration/building/edit/{id}",
new { controller = "Configuration", action = "EditBuilding" });
By leaving the id off the route defaults we make it required. I'm assuming this, because I'm guessing the Url /Building/Edit doesn't logically map to anything.
As a side node - including verbs in your urls isn't really in keeping with REST methodology, however you're not the first to do it by a long way (I include myself in that too). That said - trying to keep to it usually makes your life a lot easier, as you'll find your controllers will be cleaner, as will your route table, and your site's URL space will be a lot smaller and more obviously hierarchical. This last point is - handy for zooming around the site at dev time, but more importantly it's crucial for SEO.
So (I've commented this code heavily, hopefully enough to provide some nuggets of knowledge!):
public class ConfigurationController{
////HTTP GET /Buildings
/// DISPLAYS BUILDINGS
public ActionResult Buildings(){
//get model and return view that shows all buildings with perhaps a
//partial in that for creating a new one (or you can use another action)
//The HTML form on that view will POST to the URL handled by the method below.
}
////HTTP POST /Buildings
/// CREATES A NEW BUILDING
//use ActionName here to make this and the method above accessible through
//the same URL
[ActionName("Buildings")]
[HttpPost]
public ActionResult CreateBuilding(BuildingModel model){
//validate the model, create the object and return the same
//view as the Buildings() method above (after re-loading all the
//buildings. Or, you can issue a redirect, effectively transferring
//control back to the method above.
}
////HTTP GET /Configuration/Building/id
///DISPLAYS A BUILDING
public ActionResult Building(int id){
//get building and return view, which also contains Edit functionality
}
////HTTP POST /Configuration/Building/id
///EDITS A BUILDING
[HttpPost]
public ActionResult Building(int id, BuildingModel model){
//very similar to the CreateBuilding method - and again you might
//either simply return a building view at the end, or redirect
//to the method above.
//Note that we don't need [ActionName] here because this and the
//get method can have the same method names, because they are overloads
//i.e. since they have different method signatures we can call them the same
//thing in code.
}
}
I've left off the group stuff to keep it short, and hopefully you'll be able to see how to do it from there.
With this in place, we only need at most two routes in Global.asax.cs - although I think the order will be important:
//handles both GET and POST to this URL, i.e. our view & edit operations
routes.MapRoute("IndividualBuilding", "/configuration/buildings/{id}",
new { controller = "Configuration", action = "Building" });
routes.MapRoute("Buildings", "/configuration/buildings",
new { controller = "Configuration", action = "Buildings" });
Now we are using the HTTP verbs to signify what we intend to do with a particular request, and our URLs have become more 'logical'.
Another refactor
If you want to be 'clever' you can lump both buildings and groups under two routes
//handles both GET and POST to this URL, i.e. our view & edit operations
routes.MapRoute("Individual", "/configuration/{controller}/{id}",
new { controller = "Configuration", action = "List" });
//again, handles GET and POST
routes.MapRoute("Collection", "/configuration/{controller}",
new { controller = "Configuration", action = "Single" });
Now you do both buildings and groups controllers as I showed above, but replace Buildings (remembering the ActionName attribute on the second method) with List and Building with Single.
One final thing to consider is that because of the default MVC route:
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Default", action="Home", id = UrlParameter.Optional });
Both of your two controllers can still be routed via /Buildings/Single/1 or /Groups for example. This is a minor issue (dupe content isn't great SEO) but it can be something that people can use to sniff your site.
If you absolutely want to prevent this other url format; you can take out the default route, meaning you'd have to explicitly route other stuff that might already work (not a great issue).
Or you can use a little trick that will make it far harder: use explicit [ActionName] attributes with characters in the route name that won't be allowed through IIS - e.g. ":Single" or ":List", and then adjust our two routes from a couple of code blocks back accordingly.
So firstly you can create a controller action called AddBuilding() as you have hinted.
Then in your Global.asax file in the RegisterRoutes method you can add a route like so:
routes.MapRoute(
"AddBuilding", // Route name
"configuration/building/add", // URL with parameters
new { controller = "Configuration", action = "AddBuilding" }
);
You should not though that you will likely still be able to access the page using "/configuration/addbuilding" because of your default route mapping.
You edit one will be similar expect you will want to map the ID value for this:
routes.MapRoute(
"EditBuilding", // Route name
"configuration/building/edit/{id}", // URL with parameters
new { controller = "Configuration", action = "AddBuilding", id = UrlParameter.Optional }
);
I think you will need to add this code with the default MapRoute setup to ensure that one does not take priority
Another approach would be to create a Configuration MVC area, and then have a building and group controller in that Area.
You can do that by Attribute Routing in ASP.NET MVC 5. Something like following;
// eg: /reviews
[Route(“reviews”)]
public ActionResult Index() { … }
// eg: /reviews/5
[Route(“reviews/{reviewId}”)]
public ActionResult Show(int reviewId) { … }
// eg: /reviews/5/edit
[Route(“reviews/{reviewId}/edit”)]
public ActionResult Edit(int reviewId) { … }
You can add multiple route for the same controller as well. For details please check here
very simple basic question
I have only route:
routes.MapRoute(
"Widget", // Route name
"Widget/Frame/{postUrl}", // URL with parameters
new { controller = "Widget", action = "Index", postUrl = UrlParameter.Optional } // Parameter defaults
);
And when i try to open following url:"http://localhost:50250/Widget/Frame/qwerty"
I have an error:
The view 'qwerty' or its master was not found or no view engine
supports the searched locations. The following locations were
searched:
Well...why?
Controller code:
public class WidgetController : Controller
{
//
// GET: /Widget/
public ActionResult Index(string postUrl, int? blogEngineType)
{
return View(postUrl);
}
}
You are returning a View with
return View(postUrl);
Since there is no name of the view (in this overload), the method uses the Action name as view name and looks for it. You probably meant to do
return Redirect(postURL);
I would hazard a guess and say it's because it's actually trying to use the action name of Index(), since that's the default action that you've specified. You're not passing an {action} parameter through the url, so where else will it get the action from?
Can you change your url pattern to Widget/{action}/{postUrl} and see if it works then?
Either that, or set the default value of action to Frame instead. Basically, it has no way of knowing that you're looking for the Frame action, so it fails.
Edit: I see what you're doing now - the action's name is actually Index, right? In that case, I'm not sure, we need to see your controller code. I'll leave the above answer in case it's useful.
Edit 2: You're passing the value "qwerty" as the view name - do you have a view named "qwerty" in the views folder?
If you intend for it to be the model, and for the view name to be "Index", you should call return View((object)postUrl); instead, so that it doesn't get confused.
It's because your return statement is return View(postUrl); and when you pass a string to the View() method it is interpreted as the name of the view to use. So it looks for a view called qwerty since that's what's in that variable. If you want to hand postUrl as a model to the view of your Index action, you'll have to change your return to be return View("Index", postUrl)
Are you sure there is a View called 'qwerty' in the Shared or Widget folder within the Views parent folder?
Otherwise you probably want to use return RedirectToAction(postURL);
I am looking to produce an MVC site which has complete control of the url structure using routing.
routes.MapRoute(
"BlogView", // Route name
"view/{blogurl}", // URL with parameters
new { controller = "view", action = "view", productLink = ""} // Parameter defaults
);
routes.MapRoute(
"ProductGrid", // Route name
"category/{category}", // URL with parameters
new { controller = "category", action = "Index", category = "" } // Parameter defaults
);
I currently have the follwoing urls;
www.myblog.com/view/first-post
www.myblog.com/view/another-post
www.myblog.com/category/code
www.myblog.com/category/example
The first two urls relate to the detail view, the latter two relating ot a category view.
I have a database with the following structure; I ensure that the url (chrUrl) is a unique key.
url ( idurl (int),
chrURL,
chrAction,
chrController
)
My plan is that it is possible to look up rewrite the route lookup table so that the follwoing urls redirect to the correct view and page in the site;
www.myblog.com/first-post
www.myblog.com/another-post
www.myblog.com/code
www.myblog.com/example
Is this possible? Perofmance aside, is there a problem with this and how shoudl I go about this?
Since you don't have anything to differentiate between view and category items, I'd think about using a default controller which checks if the id is in the categories table and passes control to either the View or the Category controller.
routes.MapRoute(
"Root", // Route name
"/{id}", // URL with parameters
new { controller = "default", action = "redirect"} // Parameter defaults
);
But if you can live with having "/category/" in your category urls, that will be the more elegant solution on the back end.
First up, I would suggest coming up with a URL scheme that you are happy with. (seems you have one already)
Then I would use a ControllerFactory that will be responsible of Instantiating and
running the right action on the right controller. That is independent of any routes that you define in your route table - in fact it wont matter what you have there since you want your URL to be "database driven". You invoke the controller factory from your Global.asax file :
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new Controllers.ControllerFactory());
}
Then in the GetControllerType method in your ControllerFactory, you inspect the URL with
RequestContext.RouteData.Values.ContainsKey("keyname")
to work out the url scheme the user is presenting, and do a database look-up based on that.
If you want to take this one step further, your database can also contain references to the controller to instantiate, but that would be an overkill in your situation. As a quicknote, we use that in a solution where it was important to provide the ability for non-developers to create templates without involving dev - the database held url schemes, controller and views to render on that controller.
While you are at it, if you want to make things more elegant, create a BaseController that your controllers inherit from, and in there set things in your ViewData such as your SEO tags (MetaDescription, Title, etc) - look these up from your database.