My knowledge of MVC and Razor is quite basic so I'm hoping its something rather simple. Basically, I have my Controllers
as normal but my Views
folder has a nested structure. For example, instead of:
Views -> Index.cshtml
It is like
Views -> BrandName -> Index.cshtml
I created a custom helper to work around this, but I'm not sure how it would work with query string urls? As an example here is a controller:
private DataService ds = new DataService();
//
// GET: /Collections/
public ActionResult Index()
{
return View();
}
//
// GET: /Collections/Collection?id=1
public ActionResult Collection(int id)
{
var collectionModel = ds.GetCollection(id);
return View(collectionModel);
}
But how do I get ActionResult Collection
to look at:
Views -> Brand2 -> Collection.cshtml
Here is the workaround method I was using:
public static string ResolvePath(string pageName)
{
string path = String.Empty;
//AppSetting Key=Brand
string brand = ConfigurationManager.AppSettings["Brand"];
if (String.IsNullOrWhiteSpace(brand))
path = "~/Views/Shared/Error.cshtml"; //Key [Brand] was not specified
else
path = String.Format("~/Views/{0}/{1}", brand, pageName);
return path;
}
Use the following
public ActionResult Collection(int id)
{
var collectionModel = ds.GetCollection(id);
return View("/Brand2/Collection", collectionModel);
}
The above code will search for the following views.
~/Views/Brand2/Collection.aspx
~/Views/Brand2/Collection.ascx
~/Views/Shared/Brand2/Collection.aspx
~/Views/Shared/Brand2/Collection.ascx
~/Views/Brand2/Collection.cshtml
~/Views/Brand2/Collection.vbhtml
~/Views/Shared/Brand2/Collection.cshtml
~/Views/Shared/Brand2/Collection.vbhtml
or to be more direct
public ActionResult Collection(int id)
{
var collectionModel = ds.GetCollection(id);
return View("~/Brand2/Collection.cshtml", collectionModel);
}
Now, I want to be the first to warn you that you should never, never, never use this answer. There is a good reason for following the conventions inherent in an MVC application. Placing your files in known locations makes it easier for everyone to understand your application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With