MVC 5: ASPX and Razor View Engine in Visual Studio 2013 Update 4

只愿长相守 提交于 2019-12-25 04:22:46

问题


I am still learning and today I came to a point when I don't understand why something that should work, actually doesn't.

My sample project, I wanted to test both aspx and razor engine so i created razor view which works like a charm and now it turned out that there is no way for me to add ASPX View to my project and make it work:

(I want to make it work in MVC 5 not MVC 4)

  1. There is no option for me to create strongly typed view using ASPX engine at all:

  1. When I create it manually:

Add .aspx file to Views folder like this:

And put the code that would be generated if I was using MVC 4:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MVCDemo.Models.Employee>>" %>

<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>List</title>
</head>
<body style="font-family:Arial">

    <p>
        <% Html.ActionLink("Create New", "Create") %>
    </p>
    <table class="table" border="1">
        <tr>
            <th>
                <% Html.DisplayNameFor(model => model.Name) %>
            </th>
            <th>
                <% Html.DisplayNameFor(model => model.Gender) %>
            </th>
            <th>
                <% Html.DisplayNameFor(model => model.City) %>
            </th>
            <th>
                <% Html.DisplayNameFor(model => model.Department.Name) %>
            </th>
            <th></th>
        </tr>

        <% foreach (var item in Model) { %>
            <tr>
                <td>
                    <% Html.DisplayFor(modelItem => item.Name) %>
                </td>
                <td>
                    <% Html.DisplayFor(modelItem => item.Gender) %>
                </td>
                <td>
                    <% Html.DisplayFor(modelItem => item.City) %>
                </td>
                <td>
                    <% Html.DisplayFor(modelItem => item.Department.Name) %>
                </td>
                <td>
                    <% Html.ActionLink("Edit", "Edit", new { id = item.Id }) %> |
                    <% Html.ActionLink("Details", "Details", new { id = item.Id }) %> |
                    <% Html.ActionLink("Delete", "Delete", new { id = item.Id }) %>
                </td>
            </tr>
        <% } %>

    </table>
</body>
</html>

and the View is accessed like this:

    public ActionResult Index()
    {
        var employees = db.Employees.Include(e => e.Department);
        return View("List", employees.ToList());
    }

I am getting error, which means basically:

TRANSLATION:

*Server error in Application: '/MVCDemo'.

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Could not load type: 'System.Web.Mvc.ViewPage>'. Source error:*

  1. I tried as well to use this package from nuget, but to be honest I can't find any documentation about it.

=== ADDITIONAL INFO: ===

  1. System.Web.MVC is present

  2. Exactly the same application with Razor View works perfectly fine

I would be really grateful for your help guys.

=== SOLUTION IN CASE SOMEONE HAS SIMILAR PROBLEM ===

So, I managed figure it out, however there is still problem that Visual Studio for new projects support only razor. I wanted to try sth else, Spark for example. But it seems I cannot even import it anymore for MVC 5, I can do it only for MVC 4 project. And creating custom project template would require massive ammount of work I suppose to make it all work together.

Anyway, solution for curious ones:

  1. I had to add this to my main web.config file, to system.web section

Alternatively add this, to my .aspx View (on the very beginning):

<%@ Import Namespace="System.Web.Mvc.Html"%>
  1. I had to add this, to my web.config file in 'Views' directory (to system.web section):

  1. Finally in the ASPX View, I had to add ":", like this:

And thank you all so much for quick replies, I really appreciate your help guys.

来源:https://stackoverflow.com/questions/28550062/mvc-5-aspx-and-razor-view-engine-in-visual-studio-2013-update-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!