We have some custom MVC controller that dynamically loads different views with view name being specified through action parameter. It was working fine. Now we are going through a website localization project translating most of our UI to another language.
We will deploy the same codebase to 2 different websites, and the WEB.CONFIG culture settings would dictate the UI language for each site. We used resources for most of the site localization but for this "dynamic" controller we have decided to use different approach. For each dynamic view (let's say DynamicView.cshtml) we would also create a .RU equivalent like DynamicView.ru.cshtml. We also want it to gracefully fall back to ENGLISH version of the view if RU version was not found.
So here is the code:
public class DCController : MyControllerBase
{
public ActionResult Index(string ViewName)
{
if (Utils.IsEnglish())
return View(ViewName);
else
{
var vru = ViewName + ".ru";
var fv = ViewEngines.Engines.FindView(this.ControllerContext, vru, null);
if (fv.View == null)
// .ru view was NOT found, so let's search WITHOUT .RU (search for "English" version of this view)
return View(ViewName);
return View(vru); // How can I use "fv" object here to extract and return desired ViewResult object ????...
}
}
}
The code above works as desired. However, what bugs me is that I was not able to use already initialized ViewEngineResult fv variable to extract desired ViewResult to be returned as ActionResult - please see the very last line of code.
So the question is how can I extract ViewResult object from ViewEngineResult that was obtained through ViewEngines.Engines.FindView ???
Please advice!
I think you're confusing ViewResult
/ActionResult
with ViewEngineResult
(result of view engine view search with an IView object in it).
UPD: return View(fv.View);
might be what you're looking for there.
Yes I am somewhat confused by definition b/c I've asked for help - but the original question remains unsolved. Let me try to explain it in different words:
ViewEngineResult
, the variable is called fv
in the code abovefv
instance to get ViewResult
object (that needs to be returned by controller method)fv.View
b/c it is an IView
interface and it cannot be casted to ViewResult
return View(vru)
wgere vru is just a string, so in theory the view engine would be performing another search based on that string - but we have already performed that search earlier and we even have a valid ViewEngineResult
- but I cannot figure out how to use it (see above), so was my question - how can I get ViewResult from ViewEngineResult ....Did not notice you used a string there. You should use return View(fv.View)
instead. You can't really cast View
to ViewResult
, but you can build a ViewResult
from a View
. return View(fv.View)
should do exactly that and there's no duplicated view lookup.
Aaahhhh you are SO right :) I was looking into ViewResult
constructors, but I should have been looking into Controller.View()
overloads (link to MSDN), and sure thanks to your hint here is the one that takes IView
as a param
View(IView) - Creates a ViewResult object that renders the specified IView object.
Now if you could only put your Bitcoin address in your profile ... I want to send $2 your way - I hope you do not mind:) Thanks for helping out :)
Thanks for your help! Here's some coffee coins from me :) Not much but in many parts of our planet $2 could buy a lunch. Bitcoin knows no borders, so I hope programmers / students from other countries would eventually join our site, and Bitcoin Tipping Culture amplified through internet would help shifting a global wealth distribution towards a more fare curve with longer and richer tails. I believe we could bring a new dimension to the social interactions on the internet
FavScripts.com is a free tool to save your favorite scripts and commands, then quickly find and copy-paste your commands with just few clicks.
Boost your productivity with FavScripts.com!