【问题标题】:Can't view .aspx page in my MVC 3 Web Application无法在我的 MVC 3 Web 应用程序中查看 .aspx 页面
【发布时间】:2013-07-10 15:23:41
【问题描述】:

我有一个 MVC 3 应用程序,并且我创建了一个 .aspx 页面,我试图将其合并到我的项目中。 .aspx 页面如下所示:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin.aspx.cs"    Inherits="TabletWebApp.Views.Home.Admin" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
            DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="#333333" 
            GridLines="None" onselectedindexchanged="GridView1_SelectedIndexChanged">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="machineName" HeaderText="machineName" 
                    SortExpression="machineName" />
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:TabletWebAppConnectionString %>" 
            SelectCommand="SELECT [ID], [machineName] FROM [MachineNames] ORDER BY [ID]"></asp:SqlDataSource>
    </div>
    <asp:GridView ID="GridView2" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
        DataKeyNames="ID" DataSourceID="SqlDataSource2" ForeColor="#333333" 
        GridLines="None" onselectedindexchanged="GridView2_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="parameterName" HeaderText="parameterName" 
                SortExpression="parameterName" />
            <asp:BoundField DataField="machineID" HeaderText="machineID" 
                SortExpression="machineID" />
            <asp:BoundField DataField="minVal" HeaderText="minVal" 
                SortExpression="minVal" />
            <asp:BoundField DataField="maxVal" HeaderText="maxVal" 
                SortExpression="maxVal" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:TabletWebAppConnectionString %>" 
        SelectCommand="SELECT [ID], [parameterName], [machineID], [minVal], [maxVal] FROM [MachineParameters] ORDER BY [ID], [machineID], [parameterName]">
    </asp:SqlDataSource>
    </form>
</body>
</html>

在控制器中我只是这么称呼它:

public ActionResult Admin()
        {
            return View();
        }

但是当我尝试查看页面时,我收到错误消息“'~/Views/Home/Admin.aspx' 处的视图必须派生自 ViewPage、ViewPage、ViewUserControl 或 ViewUserControl。”!

Picture of resulting error message

我一直在谷歌搜索,发现了这个Using the ASP.NET MVC source code to debug your app

但是一旦我删除了 system.web.mvc 引用,我的项目就会出现构建错误。有什么想法吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    您正在为该页面使用 WebForms,而不是 MVC,因此 View() 将不起作用。在您的应用中拥有 WebForms 页面是完全合法的,但您无法通过 MVC 方式访问它。

    您似乎无法将 WebForms 页面放在您的 Views 目录中,即使可以,您也不应该这样做。您需要将它放在您的网络应用程序的其他位置 - 比如/HomePages

    然后你只需要做一个直接的重定向,比如:

    public ActionResult Admin() {
        return Redirect("~/HomePages/Admin.aspx");
    }
    

    【讨论】:

    • 刚刚将它移到一个新文件夹中,它就像一个魅力,谢谢!
    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    相关资源
    最近更新 更多