2

\r\n 出现在字符串上?

尝试使用Rendering ASP.NET MVC Views to String上的示例将 VIEW 作为字符串传递,并作为电子邮件发送。哪个应该通过电子邮件将销售发票发送给用户。

我已将 ViewRenderer 类添加到我的项目中。然后将 ContactSeller 函数添加到我的控制器中,并将发票视图复制并重命名为 ViewOrderThroughEmail.cshtml

//confirm payment & send invoice as email:
SendOrderInvoiceAsEmail(Qbcust, orderId);

private void SendOrderInvoiceAsEmail(QBCustomerRecord QBcust, int orderId)
{
    string email = QBcust.EmailAddress;
    if (String.IsNullOrWhiteSpace(email))
    {
        _orchardServices.Notifier.Error(T("No E-mail exists for this user. Invoice not sent."));
        return;
    }

    var siteUrl = "Confirmation of order";

    if (_authenticationService.GetAuthenticatedUser() == null)
        throw new OrchardSecurityException(T("Login Required"));

    IUser userInfo = _authenticationService.GetAuthenticatedUser();

    QBCustomerRecord customer = _qbcustomerService.GetById(QBcust.Id);

    IEnumerable<OrderRecord> ordersQuery = _orderService.GetOrders(customer.Id);

    OrderRecord order = ordersQuery.Where(o => o.Id == orderId).FirstOrDefault();

    var ids = order.Details.Select(d => d.ProductId);

    ConvertOrderViewToStringViewModel model = new ConvertOrderViewToStringViewModel { Order = _orderService.GetOrder(orderId), Products = ProductHelper.GetProductLookup(ids, _qbProductService, true) };
    model.Order = _orderService.GetOrder(orderId);

    OrderSignatureRecord signatureRecord = _signatureService.GetByOrderId(orderId);
    if (signatureRecord != null)
    {
        model.PathToSignature = _mediaService.GetPublicUrl(signatureRecord.PathToImageFile);
                model.TypedName = signatureRecord.TypedName;
    }

    model.Payment = _paymentService.GetPaymentByOrderId(orderId);

    string orderInvoiceEmailAsString = ConvertViewToString(model);
    _userService.SendEmail(email, siteUrl, orderInvoiceEmailAsString);

    _orchardServices.Notifier.Information(T("The user will receive a confirmation of their order through email."));
}

public string ConvertViewToString(ConvertOrderViewToStringViewModel model)
{   
    string message = ViewRenderer.RenderView("~/Modules/StormTree/Views/Account/ViewOrderThroughEmail.cshtml",model, ControllerContext);

    return message;
}

我已经完成了发送电子邮件功能,并且通过测试我知道发送电子邮件不是问题。电子邮件包含的内容是问题所在。

public bool SendEmail(string usernameOrEmail, string title, string viewAsString)
{
    var lowerName = usernameOrEmail.ToLowerInvariant();
    var user = _contentManager.Query<UserPart, UserPartRecord>().Where(u => u.NormalizedUserName == lowerName || u.Email == lowerName).List().FirstOrDefault();

    if (user != null)
    {
        string nonce = CreateNonce(user, DelayToResetPassword);
        //string url = createUrl(nonce);
        string url = title;

        _messageManager.Send(user.ContentItem.Record, MessageTypes.LostPassword, "email", new Dictionary<string, string> { { "LostPasswordUrl", url } });
        return true;
    }

    return false;
}

所以我已经断点并检查了传入的'viewAsString'参数:返回:

"\r\n\r\n<div class=\"span6\">\r\n    <fieldset>\r\n        <h2 style=\"color:Red;\">Order details</h2>\r\n            <table style=\"border-bottom-color:Lime;\" class=\"table-bordered table-striped table\">\r\n                <colgroup>\r\n                    <col id=\"Col1\" />\r\n                    <col id=\"Col2\" />\r\n                    <col id=\"Col3\" />\r\n                    <col id=\"Col4\" />\r\n                    <col id=\"Col5\" />\r\n                </colgroup>\r\n                <thead>\r\n                    <tr>\r\n                        <th scope=\"col\">Code</th>\r\n                        <th scope=\"col\">Product</th>\r\n                        <th scope=\"col\">Unit Price</th>\r\n                        <th scope=\"col\">Quantity</th>\r\n                        <th scope=\"col\">Value</th>\r\n                    </tr>\r\n                </thead>\r\n                <tbody>\r\n                        <tr>\r\n                            <td>APPBRAMSI</td>\r\n                            <td>Single cooking apples</td>\r\n                            <td>&#163;0.00</td>\r\n                            <td>3</td>\r\n                            <td>&#163;0.00</td>\r\n                        </tr>\r\n                        <tr>\r\n                            <td>BUTTER SQ</td>\r\n                            <td>Butternut Squash</td>\r\n                            <td>&#163;0.00</td>\r\n                            <td>5</td>\r\n                            <td>&#163;0.00</td>\r\n                        </tr>\r\n                        <tr>\r\n                            <td>bcf5</td>\r\n                            <td>Broccolli &amp; Cauliflour Mix 5kg</td>\r\n                            <td>&#163;0.00</td>\r\n                            <td>2</td>\r\n                            <td>&#163;0.00</td>\r\n                        </tr>\r\n                        <tr>\r\n                            <td>EGGS TRAY</td>\r\n                            <td>tray eggs</td>\r\n                            <td>&#163;0.00</td>\r\n                            <td>1</td>\r\n                            <td>&#163;0.00</td>\r\n                        </tr>\r\n                        <tr>\r\n                            <td>savoy single</td>\r\n                            <td>Savoy Cabbage 1*</td>\r\n                            <td>&#163;0.00</td>\r\n                            <td>9</td>\r\n                            <td>&#163;0.00</td>\r\n                        </tr>\r\n                    <tr>\r\n                        <td></td>\r\n                        <td></td>\r\n                        <td></td>\r\n                        <td><strong>Sub-Total</strong></td>\r\n                        <td><strong>&#163;0.00</strong></td>\r\n                    </tr>\r\n                    <tr>\r\n                        <td></td>\r\n                        <td></td>\r\n                        <td></td>\r\n                        <td><strong>VAT</strong></td>\r\n                        <td><strong>&#163;0.00</strong></td>\r\n                    </tr>\r\n                    <tr>\r\n                        <td></td>\r\n                        <td></td>\r\n                        <td></td>\r\n                        <td><strong>Total</strong></td>\r\n                        <td><strong>&#163;0.00</strong></td>\r\n                    </tr>\r\n                </tbody>\r\n            </table>\r\n    </fieldset>\r\n</div>\r\n\r\n<div class=\"span4\">\r\n    <fieldset>\r\n        <h2>Order information</h2>\r\n\r\n        <div class=\"editor-label\"><label for=\"Order_CreatedAt\">Order Last Updated</label></div>\r\n        <div class=\"editor-field\"><input class=\"uneditable-input\" id=\"Order_CreatedAt\" name=\"Order.CreatedAt\" readonly=\"readonly\" type=\"text\" value=\"19/12/2013 17:19:53\" /></div>\r\n\r\n        <div class=\"editor-label\"><label for=\"Order_Status\">Status</label></div>\r\n        <div class=\"editor-field\"><input class=\"uneditable-input\" id=\"Order_Status\" name=\"Order.Status\" readonly=\"readonly\" type=\"text\" value=\"New\" /></div>\r\n\r\n\r\n\r\n        <p><a href=\"/OrchardLocal/UMACS.Bradmount/CustomerAdmin/AddToCurrentOrder/171\" title=\"Add this order to Current Order\">Add this order to Current Order</a></p>\r\n        <p><a href=\"/OrchardLocal/UMACS.Bradmount/CustomerAdmin/ClearThenAddToCurrentOrder/171\" title=\"Change Current Order To Match This Order\">Change Current Order To Match This Order</a></p>\r\n        <p><a href=\"/OrchardLocal/UMACS.Bradmount/CustomerAdmin\" title=\"Back to My Account\">Go back to My Account</a></p>\r\n    </fieldset>\r\n</div>\r\n\r\n"

显示: 在此处输入图像描述 我的问题是所有的\r\n来自哪里?感谢您的任何回复

更新

public class ConvertOrderViewToStringViewModel
    {
        public ConvertOrderViewToStringViewModel()
        { }

        public OrderRecord Order;
        public Dictionary<int, QBProductRecord> Products;
        public String PathToSignature;
        public String TypedName;
        public PaymentRecord Payment;
    }
4

1 回答 1

2

可能是您的视图在 Visual Studio 中格式化时包含的空白。

因此,当您将它们转换为时ViewString它们会被替换\r\nC#.

您可以尝试从视图中删除每个空白,这不是必需的。您的视图中只有 html 和相关内容。然后通过发送电子邮件再次测试。走着瞧吧。

于 2014-01-08T10:46:39.520 回答