Nancy icon indicating copy to clipboard operation
Nancy copied to clipboard

Is there a way to render a Razor partial (without ViewStart) for an Ajax request?

Open hoppersoft opened this issue 8 years ago • 5 comments

Is there a way for the module or view to indicate that _ViewStart should not be loaded?

I see that there's a way to indicate that a page should be rendered as a partial by using HtmlHelper's Partial method, but that's for the use case where you want to inline HTML for reuse. However, I'm looking for a way for the module to indicate that no ViewStart should be used (similar to MVC's PartialViewResult).

Thanks!

hoppersoft avatar Jun 09 '16 14:06 hoppersoft

@hoppersoft Did you find any solution to this problem?

zahidmadeel avatar Nov 22 '16 14:11 zahidmadeel

I ended up modifying the Nancy/Nancy.ViewEngines.Razor code to get it working as it does in ASP.NET MVC. Thanks for reminding me; I’ve been meaning to submit a PR.

hoppersoft avatar Nov 22 '16 15:11 hoppersoft

@hoppersoft Thanks for the prompt reply. It means, currently there is no way of doing this in nancy?

zahidmadeel avatar Nov 22 '16 16:11 zahidmadeel

Nope; I had to resort to some fairly low-level hacking to make it work without breaking the existing SDHP. Once I get the PR in, you should be able to fork/patch/compile like we did.

hoppersoft avatar Nov 22 '16 16:11 hoppersoft

I came up with this solution. This is my _ViewStart.cshtml

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase

@{

    Layout = "Views/_SiteLayout.cshtml";

    if ("XMLHttpRequest".Equals(
             Request.Headers["x-requested-with"].FirstOrDefault(), 
             StringComparison.OrdinalIgnoreCase))
    {
        Layout = "Views/_Empty.cshtml";
    }

    ViewBag.Title = "Home";
}

and my _Empty.cshtml

@RenderSection("Styles", required: false)

@RenderBody()

@RenderSection("Scripts", required: false)

Steinblock avatar Jan 07 '19 15:01 Steinblock