问题描述
我是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
推荐答案
@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中的控制器调用一个简单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!