【问题标题】:Redirecting to Controller Action via UploadValues POST doesn't work通过 UploadValues POST 重定向到控制器操作不起作用
【发布时间】:2015-04-22 13:05:50
【问题描述】:

我应该从解决方案中的另一个项目调用邮件控制器的动作,动作看起来像这样:[控制器名称是ServiceMailController,动作名称是SendNotification,只是就像在调用者中一样。]

        [AllowAnonymous]
        [HttpPost]
        public ActionResult SendNotification(string MemberId, string Email, string Name, string RedirectUrl)
        {                
            bool isSent = false;
            try
            {


                Member member = MemberProvider.Instance.GetItem(ObjectId.Parse(MemberId));
                NotificationEmailModel model = new NotificationEmailModel(member);
                model.Email = Email;
                model.Name = Name;
                model.RedirectUrl = "http://example.com/" + member.UserName + "/calismalar";
                UIMailingManager.Instance.SendNotification(model);
                if(UIMailingManager.Instance.SendNotification(model).Success)
                {
                    isSent = true;
                    //comment
                }

            }            
            catch(Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            }
            return Json(isSent, JsonRequestBehavior.AllowGet);

下面是我的调用者代码,它使用 WebClient.UploadValues 通过使用“POST”方法将数据发布到上述操作。

using (var wb = new WebClient())
                            {
                                var data = new NameValueCollection();
                                data["MemberId"] = item.Member.Id.ToString();
                                data["Email"] = item.Member.Email;
                                data["Name"] = item.Member.Name;
                                data["RedirectUrl"] = "http://example.com/" + item.Member.UserName + "/calismalar";                                  

                                var resp = wb.UploadValues("http://example.com/ServiceMail/SendNotification", "POST", data);

                            }

但这给出了一个错误,根本没有详细的解释。会是什么原因?

更新:Elmah 的错误信息:

System.Web.HttpException (0x80004005):公共操作方法 在控制器上找不到“SendNotification” 'HintKumasi.UI.Controllers.ServiceMailController'。在 System.Web.Mvc.Controller.HandleUnknownAction(String actionName) 在 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) 在 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid` 1.CallEndDelegate(IAsyncResult

asyncResult) 在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤, Boolean & completedSynchronously)

【问题讨论】:

  • 您能否发布您收到的错误消息,ELMAH 说什么

标签: c# .net http-post


【解决方案1】:

创建一个类并让方法接收其中一个对象:

public class Foo
{
public string MemberId {get; set;} 
public string Email {get; set;} 
public string Name {get; set;} 
public string RedirectUrl {get; set;} 
}
public ActionResult SendNotification(Foo foo)

您的控制器上的方法正在从您的 javascript 中获取一个对象,并且无法查看属性并使用这些属性来混合您的参数。但是,如果您告诉它期望和对象,它应该能够使用传递的值解析和填充该对象。

【讨论】:

  • 我无法向Controller发送任何东西,如果它是对象或其他东西,则没有必要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多