【问题标题】:The resource cannot be found HTTP 404 Error | HttpPost | HomeController.cs找不到资源 HTTP 404 错误 |微博 |家庭控制器.cs
【发布时间】:2020-05-10 09:39:04
【问题描述】:

我正在开发我的第一个 Web 应用程序,但遇到了一个我无法解决的问题。 我正在使用 ASP.NET Web 应用程序(.NET Framework)MVC,我想在我的网站上获得一个按钮,用户可以在其中上传他们的 excel 文件,并将 excel 文件传递​​到(本地)SQL 数据库。我不能让它工作,我已经观看了 10 个 youtube 视频和 20 个不同的网站,但我不能让它工作。

这是来自 HomeController.cs 的代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace WebApplication5.Controllers
{
    public class HomeController : Controller
    {
        [HttpPost]
            public ActionResult Index(HttpPostedFileBase file)
            {
                DataSet ds = new DataSet();
                if (Request.Files["file"].ContentLength > 0)
                {
                    string fileExtension =
                                         System.IO.Path.GetExtension(Request.Files["file"].FileName);
                    if (fileExtension == ".xls" || fileExtension == ".xlsx")
                    {
                        string fileLocation = Server.MapPath("~/Content/") + Request.Files["file"].FileName;
                        if (System.IO.File.Exists(fileLocation))
                        {
                            System.IO.File.Delete(fileLocation);
                        }
                        Request.Files["file"].SaveAs(fileLocation);
                        string excelConnectionString = string.Empty;
                        excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                        //connection String for xls file format.
                        if (fileExtension == ".xls")
                        {
                            excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                        }
                        //connection String for xlsx file format.
                        else if (fileExtension == ".xlsx")
                        {
                            excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                        }
                        //Create Connection to Excel work book and add oledb namespace
                        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                        excelConnection.Open();
                        DataTable dt = new DataTable();

                        dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        if (dt == null)
                        {
                            return null;
                        }
                        String[] excelSheets = new String[dt.Rows.Count];
                        int t = 0;
                        //excel data saves in temp file here.
                        foreach (DataRow row in dt.Rows)
                        {
                            excelSheets[t] = row["TABLE_NAME"].ToString();
                            t++;
                        }
                        OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);

                        string query = string.Format("Select * from [{0}]", excelSheets[0]);
                        using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                        {
                            dataAdapter.Fill(ds);
                        }
                    }
                    if (fileExtension.ToString().ToLower().Equals(".xml"))
                    {
                        string fileLocation = Server.MapPath("~/Content/") + Request.Files["FileUpload"].FileName;
                        if (System.IO.File.Exists(fileLocation))
                        {
                            System.IO.File.Delete(fileLocation);
                        }

                        Request.Files["FileUpload"].SaveAs(fileLocation);
                        XmlTextReader xmlreader = new XmlTextReader(fileLocation);
                        // DataSet ds = new DataSet();
                        ds.ReadXml(xmlreader);
                        xmlreader.Close();
                    }
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        string conn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
                        SqlConnection con = new SqlConnection(conn);
                        string query = "Insert into Person(Name,Email,Mobile) Values('" + ds.Tables[0].Rows[i][0].ToString() + "','" + ds.Tables[0].Rows[i][1].ToString() + "','" + ds.Tables[0].Rows[i][2].ToString() + "')";
                        con.Open();
                        SqlCommand cmd = new SqlCommand(query, con);
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }

                return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

这是我的 Index.cshtml 中的代码(我认为你不需要这个,但安全总比抱歉好)

<h2>Index</h2>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />

问题是我不断收到以下错误:

我曾尝试仅运行 html 文件(因此使用默认的 homecontroller.cs 文件)并且可以正常工作,但是当我将代码添加到 Homecontroller.cs 时它停止工作。 我不知道去哪里看。 感谢您提前帮助我!

【问题讨论】:

    标签: c# html excel database


    【解决方案1】:

    您必须像下面的代码一样在 HomeController.cs 中添加 HttpGet 操作,这样您才能在浏览器中看到您的索引页面,然后能够执行任何发布操作。

    [HttpGet]
    public ActionResult Index()
    {
     return View();
    }
    

    【讨论】:

      【解决方案2】:

      您错过了在将返回视图的控制器中添加 get 方法。

      在控制器中添加以下方法,它将打开索引页面。

       [HttpGet]
       public ActionResult Index()
       {
          return View();
       }
      

      如果解决了您的问题,请标记为答案。

      【讨论】:

        猜你喜欢
        • 2013-04-25
        • 2012-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-06
        • 2021-11-16
        • 2019-04-17
        相关资源
        最近更新 更多