1

When I run the MVC request in a usual way the ASP attributes submerged within the HTML tags are correctly interpreted.

E.g.

This is what I have in the view,

        <ul class="nav navbar-nav">
            <li><a asp-controller="Home" asp-action="Index">Home</a></li>
            <li><a asp-controller="Home" asp-action="About">About</a></li>
            <li><a asp-controller="Home" asp-action="Contact">Contact</a></li>
        </ul>

This is what it translates to when it comes to the browser,

<ul class="nav navbar-nav">
      <li><a href="Home/Index">Home</a></li>
      <li><a href="/Home/About">About</a></li>
      <li><a href="/Home/Contact">Contact</a></li>
</ul>

Now, if I render the view through the following function,

    public string RenderPartialViewToString(string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = ActionContext.ActionDescriptor.Name;

        ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            var engine = Resolver.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
            ViewEngineResult viewResult = engine.FindPartialView(ActionContext, viewName);

            ViewContext viewContext = new ViewContext(ActionContext, viewResult.View, 
            ViewData, TempData, sw, new HtmlHelperOptions());

            var t = viewResult.View.RenderAsync(viewContext);
            t.Wait();

            return sw.GetStringBuilder().ToString();
        }
    }

The attributes submerged within the anchor tags remain as-is, looks like they aren't interpreted by the view engine at all.

            <ul class="nav navbar-nav">
                <li><a asp-controller="Home" asp-action="Index">Home</a></li>
                <li><a asp-controller="Home" asp-action="About">About</a></li>
                <li><a asp-controller="Home" asp-action="Contact">Contact</a></li>
            </ul>
2
  • 2
    I imagine this is because these are tag helpers, not Razor. Commented Jun 9, 2016 at 13:07
  • Hi David, yes that's true but what's the resolution to make them work. I want to retrieve the flatten HTML for everything that is part of the view, including razor and tag helpers. Commented Jun 10, 2016 at 9:58

1 Answer 1

1

I tried your RenderPartialViewToString code and everything works fine for me. My guess that when you are using RenderPartialViewToString you put your view outside from standart View folder. So please try next code

@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"   

<ul class="nav navbar-nav">
    <li><a asp-controller="Home" asp-action="Index">Home</a></li>
    <li><a asp-controller="Home" asp-action="About">About</a></li>
    <li><a asp-controller="Home" asp-action="Contact">Contact</a></li>
</ul>

So if I am right and everything works fine after that you could try add _ViewImports.cshtml file into that folder and write @addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers" there so all view in that folder or subfolders will apply same logic

Sign up to request clarification or add additional context in comments.

3 Comments

Hi YuriyP, thanks for your inputs. I have a view under standard Views directory which further invokes 2 view components, whose cshtml is part of Views/Shared/Components/ directory. The above hyperlinks are part of one of view component's cshtml. When I add tag helper, it gives me empty href i.e. <li><a href="">Home</a></li>
Core generate empty href for those controllers/action which does not exists in it routing table. So please double check if you really have public Index method in your HomeController. Also it might be a problem if you are using areas (so in that case it should be specified). Also if you are using standart routing configuration then you can receive something like that <a href="/">Home</a> and this is correct result because Home is default controller, and Index is default action.
You are right!! the place where I am having the view and the renderview method mainly exposes the HTML through web api. The client website that consumes the web api had the HomeController with action methods. As a solution, I needed to create the route map in the web api project plus HomeController with dummy action methods. Now it works fine. Thank you so much for providing the guidance to the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.