相关疑难解决方法(0)

如何将ASP.NET MVC视图呈现为字符串?

我想输出两个不同的视图(一个作为将作为电子邮件发送的字符串),另一个显示给用户的页面.

这是否可以在ASP.NET MVC beta中使用?

我尝试过多个例子:

1. ASP.NET MVC Beta中的RenderPartial到String

如果我使用此示例,则会收到"在HTTP标头发送后无法重定向".

2. MVC框架:捕获视图的输出

如果我使用它,我似乎无法执行redirectToAction,因为它尝试渲染可能不存在的视图.如果我确实返回了视图,那么它完全搞砸了,看起来根本不正确.

有没有人对我遇到的这些问题有任何想法/解决方案,或者对更好的问题有任何建议?

非常感谢!

以下是一个例子.我要做的是创建GetViewForEmail方法:

public ActionResult OrderResult(string ref)
{
    //Get the order
    Order order = OrderService.GetOrder(ref);

    //The email helper would do the meat and veg by getting the view as a string
    //Pass the control name (OrderResultEmail) and the model (order)
    string emailView = GetViewForEmail("OrderResultEmail", order);

    //Email the order out
    EmailHelper(order, emailView);
    return View("OrderResult", order);
}
Run Code Online (Sandbox Code Playgroud)

Tim Scott接受的答案(由我改变并格式化):

public virtual string RenderViewToString(
    ControllerContext controllerContext,
    string viewPath, …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc rendering

473
推荐指数
9
解决办法
29万
查看次数

在测试初始化​​方法中模拟HttpContext.Current

我正在尝试将单元测试添加到我构建的ASP.NET MVC应用程序中.在我的单元测试中,我使用以下代码:

[TestMethod]
public void IndexAction_Should_Return_View() {
    var controller = new MembershipController();
    controller.SetFakeControllerContext("TestUser");

    ...
}
Run Code Online (Sandbox Code Playgroud)

使用以下帮助程序来模拟控制器上下文:

public static class FakeControllerContext {
    public static HttpContextBase FakeHttpContext(string username) {
        var context = new Mock<HttpContextBase>();

        context.SetupGet(ctx => ctx.Request.IsAuthenticated).Returns(!string.IsNullOrEmpty(username));

        if (!string.IsNullOrEmpty(username))
            context.SetupGet(ctx => ctx.User.Identity).Returns(FakeIdentity.CreateIdentity(username));

        return context.Object;
    }

    public static void SetFakeControllerContext(this Controller controller, string username = null) {
        var httpContext = FakeHttpContext(username);
        var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
        controller.ControllerContext = context;
    }
}
Run Code Online (Sandbox Code Playgroud)

此测试类继承自具有以下内容的基类:

[TestInitialize]
public void Init() {
    ...
} …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing mocking httpcontext

171
推荐指数
4
解决办法
12万
查看次数

是否可以从Web API访问HttpContext.Current.Session

是否可以通过WebAPI访问HttpContext.Current.Session?我们可以让它继承IRequiresSession吗?

我有一个通用处理程序在我想要删除的API调用之后执行会话集.

public void AccountController : ApiController, IRequiresSessionState
{
public void Login()
{
setsession(){}
} 
}
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-web-api

27
推荐指数
2
解决办法
4万
查看次数

Autofac 范围生命周期问题

我有 ASP.NET MVC 应用程序,我在其中注册了一个具有InstancePerHttpRequest作用域的组件。

builder.RegisterType<Adapter>().As<IAdapter>().InstancePerHttpRequest();
Run Code Online (Sandbox Code Playgroud)

然后我有一段异步代码,我正在解析适配器组件。

下面的代码是简化的

Task<HttpResponseMessage> t = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t =>

      // IHandleCommand<T> takes an IAdapter as contructor argument
      var h = DependencyResolver.Current.GetServices<IHandleCommand<T>>();
);
Run Code Online (Sandbox Code Playgroud)

上面的代码抛出异常: The request lifetime scope cannot be created because the HttpContext is not available.

所以我对这个主题做了一些研究,找到了这个答案 /sf/answers/606411501/

然后我将解析代码调整为此

 using (var c= AutofacDependencyResolver.Current.ApplicationContainer.BeginLifetimeScope(x => x.RegisterType<DataAccessAdapter>().As<IDataAccessAdapter>).InstancePerLifetimeScope()))
 {
       var h = DependencyResolver.Current.GetServices<IHandleCommand<T>>();
 }
Run Code Online (Sandbox Code Playgroud)

但例外情况保持不变。 The request lifetime scope cannot be created because the HttpContext is not available.

我错过了什么吗?

autofac

5
推荐指数
1
解决办法
4016
查看次数