如何在ASP.NET MVC3中使用参数路由URL

时间:2014-02-11 11:48:09

标签: c# asp.net-mvc-3 url-routing url-parameters

我是ASP MVC3的新手。 我需要从我的视图中将参数传递给控制器​​

viewgrid.aspx

 <form id="form1" runat="server">
    <h3>
        GridView DataBind
    </h3>
    <asp:Label ID="Message" ForeColor="Red" runat="server" />
    <br />
    <asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="IdGroupe" HeaderText="Group ID" />
            <asp:BoundField DataField="IdFichier" HeaderText="File ID" />
            <asp:BoundField DataField="NomFichier" HeaderText="File Name" />
            <asp:BoundField DataField="DateHeureAjout" HeaderText="Adding Date" />
            <asp:HyperLinkField Text="Download" DataNavigateUrlFields="IdFichier,IdGroupe" DataNavigateUrlFormatString="~\Home\Download.aspx?IdF={0}&IdG={1}"
                HeaderText="Click To Download" Target="About" />
        </Columns>
    </asp:GridView>
    </form>

HomeController.cs

        public ActionResult viewgrid(string id)
        {
            return View();
        }

        public ActionResult Download(string IdF, string IdG)
        {
            SqlConnection cnx = new SqlConnection("Server=****;User=****;Pwd=***;MultipleActiveResultSets=True");
            SqlCommand cmd = new SqlCommand("SELECT Fichier.NomFichier FROM Fichier WHERE Fichier.IdFichier ='" + IdF + "' AND Groupe.IdGroupe='" + IdG + "'");
            cnx.Open();
            SqlDataReader r = cmd.ExecuteReader();
            while (r.Read())
            {
                string Nfichier = r.GetString(0);
                WebClient webClient = new WebClient();
                webClient.DownloadFile("~/FileUploaded/" + Nfichier + "", @"c:\" + Nfichier + "");
            }
            return RedirectToAction("Index");
        }

的Site.Master

<div id="Div1">
    <ul id="Ul1">
        <li>
            <%: Html.ActionLink("Home", "Index", "Home")%></li>
        <li>
            <%: Html.ActionLink("About", "About", "Home")%></li>
        <li>
            <%: Html.ActionLink("Home", "viewgrid", "Home")%></li>
        <li>
            <%: Html.ActionLink("Home", "Download", "Home", new{iIdF={0}&IdG={1}})%></li>
    </ul>
</div>

但我的服务器无法识别通过的网址,我知道这是一个路由问题,但我无法找到解决方案。

1 个答案:

答案 0 :(得分:0)

由于HttpPost属性,您的下载操作将仅处理POST请求:

[HttpPost]
public ActionResult Download(string IdF, string IdG)

Html.ActionLink生成链接。浏览器在单击链接时发送GET请求。删除[HttpPost]属性以处理GET请求。