如何在mvc3中获取CURL值


how to get CURL values inside mvc3?

我是MVC的新手。我使用下面的程序,使用asp.net MVC3和sql server 2005。我使用以下代码,它工作正常。我的问题是如何更改这个程序,比如,如何从CURL(php)中获取值,而不是注册表单值,并将这些值添加到sql server 2005中?

UserManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcDemo.Models.DB;
using MvcDemo.Models.ViewModels;
namespace MvcDemo.Models
{
    public class UserManager
    {
        private UserView obj = new UserView();
        public void Add(UserView user)
        {
            obj.FirstName = user.FirstName;
            DB.Signup sysUser = new Signup();
            sysUser.FirstName = user.FirstName;
            sysUser.LastName = user.LastName;
            sysUser.Contactnumber = Int32.Parse(user.ContactNumber);
            sysUser.LoginID = user.LoginID;
            sysUser.Password = user.Password;
            obj.AddToSysUsers(sysUser);
                      }
                }
}
AccountController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using MvcDemo.Models.ViewModels;
using MvcDemo.Models;

namespace MvcDemo.Controllers
{
    public class AccountController : Controller
    {
        public ActionResult SignUp()
        {
            return View("SignUp");
        }


        [HttpPost]
        public ActionResult SignUp(UserView user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    UserManager userManager = new UserManager();
                        userManager.Add(user);
                        FormsAuthentication.SetAuthCookie(user.FirstName, false);
                        return RedirectToAction("Welcome", "Home");
                }
            }
            catch
            {
                return View(user);
            }
            return View(user);
        }
    }
}
Userview.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel.DataAnnotations;

namespace MvcDemo.Models.ViewModels
{
    public class UserView
    {

        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        [Required]
        [Display(Name = "Contact Number")]
        public string ContactNumber { get; set; }
        [Required]
        [Display(Name = "Login ID")]
        public string LoginID { get; set; }
        [Required]
        [Display(Name = "Password")]
        public string Password { get; set; }

        public void AddToSysUsers(DB.Signup sysUser)
        {
            try
            {
                string con = System.Configuration.ConfigurationManager.ConnectionStrings["traineeEntities"].ConnectionString;
                SqlConnection co = new SqlConnection(con);
                co.Open();
                String str = "Insert into Signup values('"+sysUser.FirstName+"','"+sysUser.LastName+"',"+sysUser.Contactnumber+",'"+sysUser.LoginID+"','"+sysUser.Password+"')";
                SqlCommand cmd = new SqlCommand(str, co);
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //Message=Error Creating User: User already exists
            }
        }
    }
}
Signup.cshtml
@model MvcDemo.Models.ViewModels.UserView
@{
    ViewBag.Title = "SignUp";
}
<h2>Sign-Up</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>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>UserView</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ContactNumber)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ContactNumber)
            @Html.ValidationMessageFor(model => model.ContactNumber)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.LoginID)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LoginID)
            @Html.ValidationMessageFor(model => model.LoginID)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Return to Home page","Index", "Home")
</div>

我很难理解你到底想做什么。但是,如果我理解你的意思是正确的,你想通过PHP和cURL提交注册表格。这就是您使用cURL:提交HTTP帖子的方式

<?php
$data = array(
    'FirstName' => 'The firstname value',
    'LastName ' => 'The lastname value',
    'ContactNumber ' => 'The contactnumber value',
    'LoginID ' => 'The loginid value',
    'Password ' => 'The password value'
);
$post = http_build_query($data);
$url = 'http://url_to_your_server/Account/SignUp/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
var_dump($response);

不太确定获取注册表单变量是什么意思,应该可以很容易地从传递给操作的模型中做到这一点。

如果你的意思是,如何获得模型中不存在的外部值,你需要使用。Request属性。

public ActionResult Register(...) {
    var x = Request.Form["your form value here"] // for forms
    var y = Request.QueryString["your query string value here"] // for query strings
    var z = Request.Headers["for headers"] // for headers
    ... etc ...
}

如果你能进一步澄清你的问题,你可能会得到一些更好的答案,到目前为止,我们还不清楚你的目标。