Coder Social home page Coder Social logo

Comments (4)

vyelchuri avatar vyelchuri commented on August 13, 2024

@RickStrahl Does this project contains any test cases for the "RenderViewToString" ?

"RenderViewToString" only allows to work when it contains HTTPContext, how to create a test case with out HTTPContext ?

I have tried to create fake HTTPContext, but it was failing though when FindView() is executing
HttpContext.Current = new HttpContext( new HttpRequest("", "http://tempuri.org", ""), new HttpResponse(new StringWriter()) );

from westwindtoolkit.

RickStrahl avatar RickStrahl commented on August 13, 2024

You really need an HttpContext for this to work. If you want to do this outside of ASP.NET, I recommend you look at https://github.com/RickStrahl/Westwind.RazorHosting instead which can be run in any type of project.

from westwindtoolkit.

ManfredLange avatar ManfredLange commented on August 13, 2024

Looks as if the link is broken. Here is a link that worked as of writing: Westwind.RazorHosting

from westwindtoolkit.

SGK1977 avatar SGK1977 commented on August 13, 2024

@RickStrahl - I'm trying to use your code to create a controller context from a singleton inside an MVC application. Since there is no httpcontext I am creating a dummy one like this. And then I use it inside the controller context. It seems to work, but I am not sure what the cons of using a dummy httpcontext like this are. Please advise if there is an alternative approach.

HttpContext ctx = new HttpContext(
new HttpRequest("", "http://tempuri.org", ""),
new HttpResponse(new StringWriter())
);

        // User is logged in
        ctx.User = new GenericPrincipal(
            new GenericIdentity("username"),
            new string[0]
            );

        // User is logged out
        ctx.User = new GenericPrincipal(
            new GenericIdentity(String.Empty),
            new string[0]
            );

        // get context wrapper from HttpContext if available
        HttpContextBase wrapper = null;
        if (ctx != null)
            wrapper = new HttpContextWrapper(ctx);
        else
            throw new InvalidOperationException(
                "Can't create Controller Context if no active HttpContext instance is available.");

from westwindtoolkit.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.