【问题标题】:MVC project startupMVC项目启动
【发布时间】:2018-05-11 08:08:57
【问题描述】:

我有一个现有的 MVC 项目,它有一个淘汰前端和一个 WebAPI 后端。

由于需求的变化,我不得不从项目中删除所有后端文件,只留下前端文件。

现在我需要一个 WebAPI 控制器回到项目中。

为了配置 WebAPI,我需要在 C# 代码中找到一个入口点来触发配置方法。

我的理解是,如果项目有一个 Global.asax 文件,Application_Start() 将在启动时自动触发。此外,如果我有一个带有 owinStartup 属性的 startup.cs 文件,默认情况下也会触发。

所以我从另一个项目复制了这两个文件并根据需要进行了编辑,但启动时两个文件都没有被触发。

我假设我缺少某种配置,但不确定是什么?

这是我的 startup.cs 文件:

using System.Web.Http;
using Microsoft.Owin;
using Newtonsoft.Json;
using Owin;

[assembly: OwinStartup(typeof(ClearviewLocal_ONFrontEnd_Rest.Startup))]
namespace ClearviewLocal_ONFrontEnd_Rest
{
    /// <summary>
    /// Application startup
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// Configure app
        /// </summary>
        /// <param name="app">Owin AppBuilder</param>
        public void Configuration(IAppBuilder app)
        {         
            var config = new HttpConfiguration();         //breakpoint here that is not being hit
        }
    }
}

这里是 Global.asax 文件:

using System;
using System.Web;
using System.Web.Http;

namespace ClearviewLocal_ONFrontEnd_Rest
{
    /// <summary>
    /// 
    /// </summary>
    public class WebApiApplication : HttpApplication
    {
        /// <summary>
        /// 
        /// </summary>
        protected void Application_Start()
        {
            var config = new HttpConfiguration(); //Breakpoint here            
        }
    }
}

我已经查看了这个问题的答案:OwinStartup not Starting ... Why? 但此代码从未在 Web.Config 中

另外,我没有从项目中删除任何引用

【问题讨论】:

  • 出于好奇,您必须摆脱所有“后端”文件是怎么回事?
  • @JᴀʏMᴇᴇ WebAPI 调用被移到另一个项目中,因为其他项目也需要它们

标签: c# asp.net-web-api owin global-asax


【解决方案1】:

这个问题的接受答案OwinStartup not firing 似乎已经解决了我的问题:

" 确保您已在项目中安装 Microsoft.Owin.Host.SystemWeb 包。 IIS 托管应用程序中的启动检测需要此包。 "

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 2012-04-21
    相关资源
    最近更新 更多