Asp.net web api Web Api控制器能否将视图呈现为字符串?

Asp.net web api Web Api控制器能否将视图呈现为字符串?,asp.net-web-api,Asp.net Web Api,我想写一个Web Api控制器动作,根据结果发送电子邮件。我想使用MVC视图或带有数据模型的局部视图来呈现电子邮件的正文 有办法做到这一点吗 我想要这样的东西: public class NotificationApiController : ApiController { private IMkpContext db; public string ViewNotifications() { var dataModel = GetDataModel();

我想写一个Web Api控制器动作,根据结果发送电子邮件。我想使用MVC视图或带有数据模型的局部视图来呈现电子邮件的正文

有办法做到这一点吗

我想要这样的东西:

public class NotificationApiController : ApiController
{
    private IMkpContext db;

    public string ViewNotifications()
    {
        var dataModel = GetDataModel();
        if (dataModel != null) 
        {
            SendEmail(dataModel.ToAddress, dataModel.FromAddress, dataModel.Subject, RenderBody("viewName", dataModel);
        }

        return string.Empty;
    }
}
public static class ViewUtil
{
    public static string RenderPartial(string partialName, object model)
    {
        var sw = new StringWriter();
        var httpContext = new HttpContextWrapper(HttpContext.Current);

        // point to an empty controller
        var routeData = new RouteData();
        routeData.Values.Add("controller", "EmptyController");

        var controllerContext = new ControllerContext(new RequestContext(httpContext, routeData), new EmptyController());

        var view = ViewEngines.Engines.FindPartialView(controllerContext, partialName).View;

        view.Render(new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), sw), sw);

        return sw.ToString();
    }
}

class EmptyController : Controller { }

RenderBody将在其中查找viewName,使用dataModel中的数据填充它,并将视图呈现为字符串。

如果您不想使用注释中建议的RazorEngine方法,可以定义如下类:

public class NotificationApiController : ApiController
{
    private IMkpContext db;

    public string ViewNotifications()
    {
        var dataModel = GetDataModel();
        if (dataModel != null) 
        {
            SendEmail(dataModel.ToAddress, dataModel.FromAddress, dataModel.Subject, RenderBody("viewName", dataModel);
        }

        return string.Empty;
    }
}
public static class ViewUtil
{
    public static string RenderPartial(string partialName, object model)
    {
        var sw = new StringWriter();
        var httpContext = new HttpContextWrapper(HttpContext.Current);

        // point to an empty controller
        var routeData = new RouteData();
        routeData.Values.Add("controller", "EmptyController");

        var controllerContext = new ControllerContext(new RequestContext(httpContext, routeData), new EmptyController());

        var view = ViewEngines.Engines.FindPartialView(controllerContext, partialName).View;

        view.Render(new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), sw), sw);

        return sw.ToString();
    }
}

class EmptyController : Controller { }

如果您不想使用评论中建议的RazorEngine方法,可以定义如下类:

public class NotificationApiController : ApiController
{
    private IMkpContext db;

    public string ViewNotifications()
    {
        var dataModel = GetDataModel();
        if (dataModel != null) 
        {
            SendEmail(dataModel.ToAddress, dataModel.FromAddress, dataModel.Subject, RenderBody("viewName", dataModel);
        }

        return string.Empty;
    }
}
public static class ViewUtil
{
    public static string RenderPartial(string partialName, object model)
    {
        var sw = new StringWriter();
        var httpContext = new HttpContextWrapper(HttpContext.Current);

        // point to an empty controller
        var routeData = new RouteData();
        routeData.Values.Add("controller", "EmptyController");

        var controllerContext = new ControllerContext(new RequestContext(httpContext, routeData), new EmptyController());

        var view = ViewEngines.Engines.FindPartialView(controllerContext, partialName).View;

        view.Render(new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), sw), sw);

        return sw.ToString();
    }
}

class EmptyController : Controller { }

我通常使用RazorEngine库来呈现我的视图,并将其用于我的电子邮件。它的另一个好处是允许您将视图声明为强类型。我使用邮政也是为了同样的目的。我通常使用RazorEngine库来呈现我的视图,并将其用于我的电子邮件。它的另一个好处是允许您将视图声明为强类型。我使用邮政也是为了同样的目的。我得到一个异常:错误CS0103:名称“model”在当前上下文中不存在。我想我需要其他东西来将模型传递给视图……我遇到了一个异常:错误CS0103:名称“model”在当前上下文中不存在。我想我需要其他东西来将模型传递给视图。。。。