Get ViewResult from ViewEngineResult object that was obtained through ViewEngines.Engines.FindView - ASP.NET MVC

0.5
=
2
+
0
0.5 Bitcoin bounty has been announced by author.
2 has been already awarded by author.
0 remains available.
1

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!

1 Answer

1
=
1
=
$2
1 tip with total amount of 7.7999997 mBTC($2 USD) have been sent by alex

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.

SEND BITCOIN TIPS
User rating:

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:

  • I have an initialized instance of ViewEngineResult, the variable is called fv in the code above
  • I want to use that fv instance to get ViewResult object (that needs to be returned by controller method)
  • I cannot use fv.View b/c it is an IView interface and it cannot be casted to ViewResult
  • so I ended up using 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 ....
  • the code above works, it just bugs me that ViewEngine has to perform the same view search TWICE
User rating:

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.

User rating:

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 :)

User rating:

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

User rating:

Great, thank you.

1

Too many commands? Learning new syntax?

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!

Post Answer