Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning view via a path name

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.

like image 910
stephenbayer Avatar asked Aug 28 '13 14:08

stephenbayer


People also ask

How do I return specific view on a controller?

To return a view from the controller action method, we can use View() method by passing respective parameters. return View(“ViewName”) – returns the view name specified in the current view folder (view extension name “. cshtml” is not required. return View("~/Views/Account/Register.

What is return view in MVC?

The default behavior of the View method ( return View(); ) is to return a view with the same name as the action method from which it's called. For example, the About ActionResult method name of the controller is used to search for a view file named About.cshtml .


1 Answers

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);
like image 164
Darin Dimitrov Avatar answered Sep 28 '22 02:09

Darin Dimitrov