简体   繁体   English

ASP.NET-运行.NET App

[英]ASP.NET - Running .NET App

I am learning ASP.NET Core 1.0. 我正在学习ASP.NET Core 1.0。 I followed the instructions provided at this link . 我按照此链接提供的说明进行操作。 I am able to successfully load static pages via lite-server. 我能够通过lite-server成功加载静态页面。 However, I cannot figure out how to render server-side pages. 但是,我不知道如何呈现服务器端页面。

For example, the link above explains how to get index.html appearing. 例如,上面的链接说明了如何显示index.html。 However, when I visit http://localhost:[port]/ , I kind of would expect to see the contents of Views/Home/Index.cshtml. 但是,当我访问http:// localhost:[port] /时 ,我有点希望看到Views / Home / Index.cshtml的内容。

How do I execute server-side pages? 如何执行服务器端页面?

Updated: Here is my project.json file: 更新:这是我的project.json文件:

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },
  "tooling": {
    "defaultNamespace": "MyApp"
  },

  "dependencies": {
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions" : "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
  },

  "frameworks": {
    "dnx451": {},
    "dnxcore50": {}
  },

  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],
  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ],
  "scripts": {
    "prepublish": [
      "npm install",
      "bower install",
      "gulp clean",
      "gulp min"
    ]
  }
}

startup.cs startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace MyApp
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            // Set up configuration sources.
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; set; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseIISPlatformHandler();

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

        // Entry point for the application.
        public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
    }
}

Thanks 谢谢

All of the view pages you create in the Views folder and have the extension .cshtml are loaded via a controller - generally, by convention, of the same name as the method within the controller. 您在Views文件夹中创建的所有具有扩展名.cshtml的视图页面都是通过控制器加载的-通常,按照惯例,该名称与控制器内的方法同名。 So, for one of these View pages to fire, a corresponding controller action (method) must execute. 因此,要触发这些“视图”页面之一,必须执行相应的控制器动作(方法)。

In order to get the controller's method to fire, there needs to be an appropriate route configured. 为了触发控制器的方法,需要配置适当的路由。 I didn't see in the tutorial you referenced any indication that MVC was 'enabled'. 在本教程中,我没有看到您引用了MVC已“启用”的任何迹象。 Even though you have a reference to MVC in the above project.json file, MVC must be configured within the startup.cs file. 即使在上述project.json文件中引用了MVC,也必须在startup.cs文件中配置MVC。 And a route needs to be configured. 并且需要配置路由。

What you're Yeoman tutorial has shown you is how to create an 'empty' project - not an MVC one. Yeoman教程向您展示了如何创建一个“空”项目,而不是MVC项目。

I hope this helps out :) 我希望这可以帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM