如何从MVC中的控制器调用一个简单的方法 [英] How to call a simple method from controller in MVC

查看:91
本文介绍了如何从MVC中的控制器调用一个简单的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC的新手我在我的家庭控制器中写了一个简单的短信发送方法,如下所示

I am new in MVC I have wrote a simple SMS Send Method in my home controller as shown below

[HttpPost]
      public ActionResult SendSMS()
      {
         // TheTextingDemo.Models.ConfigurationSettings s1 = new Models.ConfigurationSettings();
         // string API_KeY = s1.API_KEY;
          string API_KEY = "<removed>";
          string API_SECRET = "<removed>";
          string TO = "<removed>";
          string Message = "Hello world";
          string sURL;
          StreamReader objReader;

          sURL = "https://www.thetexting.com/rest/sms/json/message/send?api_key=" + API_KEY + "&api_secret=" + API_SECRET + "&to=" + TO + "&text=" + Message;
          WebRequest wrGETURL;
          wrGETURL = WebRequest.Create(sURL);
          try
          {
              Stream objStream;
              objStream = wrGETURL.GetResponse().GetResponseStream();
              objReader = new StreamReader(objStream);
              objReader.Close();
          }
          catch (Exception ex)
          {
              ex.ToString();
          }
        return View();
      }
      public ActionResult About()
      {
          ViewBag.Message = "Your application description page.";

          return View();
      }





在我的主页上我已经显示了一个带有一些文本框的表单我想调用这个方法我不想改变下面的视图是我的HTML



on my home page i have shown a form with some textboxes i want to call this method i do not want to change the view below is my html

@model  TheTextingDemo.Models.ConfigurationSettings


<h2>TheTexting SMS GateWay Demo</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm())
{
    <div>
        <fieldset>
            <legend>Account Information</legend>
            <div>
                @Html.LabelFor(model => model.API_KEY)
            </div>
            <div>
                @Html.TextBoxFor(model => model.API_KEY)
                @*@Html.ValidationMessageFor(model => model.API_KEY)*@
            </div>
            <div>
                @Html.LabelFor(model => model.API_SECRET)
            </div>
            <div>
                @Html.TextBoxFor(model => model.API_SECRET)
                @*@Html.ValidationMessageFor(model => model.API_SECRET)*@
            </div>
            <div>

                @Html.LabelFor(a => a.To_Number)


            </div>
            <div>

                @Html.TextBoxFor(a => a.To_Number)
                @*@Html.ValidationMessageFor(a => a.To_Number)*@
            </div>
            <div>
                @Html.LabelFor(a => a.TextMessage)


            </div>
            <div>

                @Html.TextBoxFor(a => a.TextMessage)
                @*@Html.ValidationMessageFor(a => a.TextMessage)*@
            </div>

            <br />

            <a href="@Url.Action("SendSMS", "Home")">Add an Admin</a>
          @Url.Action("SendSMS", "Home")
            <input id="Submit" type="submit" value="SendSMS" />

          
        </fieldset>
    </div>

}





如何调用此方法我尝试了多种方法,但它说



How to call this method i have tried number of ways but it says

The resource cannot be found.

天气我不想重定向到下一个视图



我尝试过:



我尝试了很多方法url.action和其他许多方法

weather i do not want to redirect to the next view

What I have tried:

I tried lot of ways url.action and many others

推荐答案

资源无法找到(404)表示您没有使用正确的URL来访问它,您的资源位于其他位置。您未正确配置应用程序,或者需要检查正在访问的URL。它们是什么,只有你知道。



我可以肯定告诉你的一件事是你需要使用 HTTP POST 请求,因为你标记了POST接受器的功能,为此你需要在表格中写这个;或者使用JavaScript将请求方法更改为POST,这将很难。试试这个,

The resource cannot be found (404) means that you are not using the correct URL to access it, your resource is located at some other location. Either you configured the application incorrectly, or you need to check the URL being accessed. What they are, only you know.

One thing that I can surely tell you is that you need to use HTTP POST request, as your marked the function as POST acceptor, for that you need to write this in the form; or using JavaScript change the request method to POST, which will be tough. Try this,
@using (Html.BeginForm("SendSMS", "Home", FormMethod.Post))



这将在使用POST动词提交时将请求发送到服务器; ASP.NET这次会接受它。请注意,它只会发送请求,但服务器端不会读取任何数据,这让我想到很多事情,比如为什么你甚至想要这样做呢?为什么不创建一个简单的接口或后端类来处理这些,你只需要这样做,


This will send the request to the server upon submission using POST verb; ASP.NET will accept it this time. Note that it will only send the request, but no data will be read on the server-side, this makes me think a lot of things, such as why do you even want to do this? Why not create a simple interface or a backend class that handles these, and you only do,

smsHelper.SendSms(); 



这样做会更加安全。如果您想进行任何进一步的更改,它还可以帮助您稍后修改代码。


That will be much safer way of doing this. It will also help you modify your code later one, if you want to make any further changes.


这篇关于如何从MVC中的控制器调用一个简单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆