Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
      }


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.
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
Posted
Updated 3-Feb-17 10:35am
v3
Comments
Afzaal Ahmad Zeeshan 3-Feb-17 16:37pm    
Remember, never ever share your phone number, email address, passwords or personal tokens or keys online. People are not good here, and they will definitely use these values for evil; spam you, use your resources, send threats to someone imitating you.

Be safe. Never share any credential, if someone wants to help, they will do so by using the resources provided, and will leave if they cannot.

1 solution

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,
C#
@using (Html.BeginForm("SendSMS", "Home", FormMethod.Post))

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,
C#
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.
 
Share this answer
 
Comments
Malikdanish 4-Feb-17 5:37am    
Thanks Afzaal Ahmad Zeeshan , basically I have a simple MVC view having four labels and four textboxes i am using API to send sms to any number so my textboxes would have values
1-API_KEY (required to send sms from API Gateway Like thetexting.com)
2-API_SECRET(also required......)
3-TO_Number ( to which i want to send sms)
4-Message(message which need to be send)
for that purposes i write simple class name configuration class
public class ConfigurationSettings
{


[Key]
[ScaffoldColumn(false)]
public int RecordID { get; set; }
// [Required(ErrorMessage = "EmployeeName is required")]
//public string EmployeeName { get; set; }
// [Required(ErrorMessage = "EmployeeID is required")]
// public int EmployeeID { get; set; }
[Required(ErrorMessage = "API_KEY is required")]
public string API_KEY { get; set; }
[Required(ErrorMessage = "API_SECRET is required")]
public string API_SECRET { get; set; }
[Required(ErrorMessage = "API_SECRET address is required")]

[Display(Name = "To_Number")]
public int To_Number { get; set; }
[Required(ErrorMessage = "To_Number is required")]

[DataType(DataType.PhoneNumber)]

public string TextMessage { get; set; }



and my view is

@model TheTextingDemo.Models.ConfigurationSettings


TheTexting SMS GateWay Demo



<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript">
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm())
{


Account Information

@Html.LabelFor(model => model.API_KEY)



@Html.TextBoxFor(model => model.API_KEY)
@*@Html.ValidationMessageFor(model => model.API_KEY)*@


@Html.LabelFor(model => model.API_SECRET)


@Html.TextBoxFor(model => model.API_SECRET)
@*@Html.ValidationMessageFor(model => model.API_SECRET)*@



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





@Html.TextBoxFor(a => a.To_Number)
@*@Html.ValidationMessageFor(a => a.To_Number)*@


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





@Html.TextBoxFor(a => a.TextMessage)
@Html.ValidationMessageFor(a => a.TextMessage)




@*@Html.ActionLink("link text", "SendSMS", "Home", null, null)
Add an Admin
@Url.Action("SendSMS", "Home")
*@





}
and my method is
public ActionResult SendSMS(ConfigurationSettings sm)
{

// TheTextingDemo.Models.ConfigurationSettings s1 = new Models.ConfigurationSettings();
//string API_KeY = Request.QueryString["txtRate"].ToString();
string API_KEY = "wwpiub1gcfk49a1";
string API_SECRET = "j5hsuac8f6mdzdd";
string TO = "923215029981";
string Message = "Hello world";
string sURL;
StreamReader objReader;
//string ssurl="https://www.thetexting.com/rest/sms/json/message/send?api_key=wwpiub1gcfka1&api_secret=j5hsf6mdzdd&to=9232199999981&text=Hello"
sURL = "https://www.thetexting.com/rest/sms/json/message/send?api_key=" + API_KEY + "&api_secret=" + API_SECRET + "&to=" + TO + "&text=" + Message;
WebRequest wrGETURL;
Malikdanish 4-Feb-17 5:58am    
When i remove the [httppost] and put a debug breakpoint then it comes to method but when i make is
httppost then it does not come to my method
Afzaal Ahmad Zeeshan 4-Feb-17 6:51am    
Yes, that is because while removing you are accepting any HTTP request, but with POST you are asking ASP.NET to only execute this action when the request is of type POST.
Malikdanish 4-Feb-17 7:00am    
I have a simple index view in which i have five text box and i have a method in home controller i want to get call this method on button click of a button placed on index method how to achieve this can i send you my code on email

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900