简体   繁体   English

在 web api 项目中使用 asp.net mvc

[英]Use asp.net mvc within a web api project

All Links I find talk about using an Web API inside a mvc project.我找到的所有链接都在谈论在 mvc 项目中使用 Web API。

But I want the opposite.但我想要相反的。

I can not find any info about that scenario and how to integrate mvc into a web api project.我找不到有关该场景以及如何将 mvc 集成到 Web api 项目的任何信息。

Is it possible at all?有可能吗?

I'm starting these instructions from an empty ASP.Net project setup with Web API.我从使用 Web API 的空 ASP.Net 项目设置开始这些说明。

  1. Install the Microsoft.AspNet.Mvc Nuget package.安装Microsoft.AspNet.Mvc Nuget 包。

  2. Set up MVC routes and any other MVC configuration in your global.asax, where you configure Web API.在您配置 Web API 的 global.asax 中设置 MVC 路由和任何其他 MVC 配置。 You can do this configuration in a separate class and call it from global.asax, if you wish.如果您愿意,您可以在单独的类中执行此配置并从 global.asax 中调用它。

Example:例子:

RouteTable.Routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
  1. Add an MVC controller to a controllers folder.将 MVC 控制器添加到controllers文件夹。 This should be a separate folder that houses your Web API controllers, and does not have to be called controllers .这应该是一个单独的文件夹,用于存放您的 Web API 控制器,并且不必称为controllers

  2. Create a Views folder.创建一个Views文件夹。 To make this a lot easier and less time consuming, create an MVC project in a throwaway solution and copy its Views folder over.为了使这更容易和更省时,在一次性解决方案中创建一个 MVC 项目并复制它的Views文件夹。 In particular, you want to copy over any files in the Views\\Shared folder, and all .config files inside the Views folder.特别是,任何你想要的文件拷贝过来的Views\\Shared文件夹,所有.config内部文件Views文件夹中。 Without these, your MVC views won't work.没有这些,您的 MVC 视图将无法工作。

  3. If you created a throwaway project for #4, look through its web.config, in particular, at the <system.web> settings.如果您为 #4 创建了一个一次性项目,请查看其 web.config,尤其是<system.web>设置。

If you didn't, here's part of mine:如果你没有,这是我的一部分:

<system.web>
   <authentication mode="None" />
   <compilation debug="true" targetFramework="4.5.2" />
   <httpRuntime targetFramework="4.5.2" />
</system.web>

You should now have MVC working in a ASP.Net project alongside Web API.您现在应该让 MVC 与 Web API 一起在 ASP.Net 项目中工作。

For more organization, you can create an MVC Area .对于更多的组织,您可以创建一个MVC 区域 To get started you don't need Areas, and if you decide later to add Areas, it's easy to move existing MVC code into one or more new Areas.开始时您不需要区域,如果您稍后决定添加区域,将现有 MVC 代码移动到一个或多个新区域很容易。

For more information, read this blog post by David Paquette .有关更多信息,请阅读David Paquette 的这篇博文 Yes, the article is about Web Forms, but the instructions and concepts are congruent.是的,这篇文章是关于 Web 表单的,但说明和概念是一致的。 Plus, Web Forms is just an ASP.Net project with Web Forms , just like Web API.另外,Web Forms 只是一个带有 Web Forms的 ASP.Net 项目,就像 Web API 一样。

It's simple and I use it on several sites - create a new Area (I use 'UI') and treat it as a simple MVC site.它很简单,我在几个站点上使用它 - 创建一个新区域(我使用“UI”)并将其视为一个简单的 MVC 站点。

You will have to fix your default routes per this: Multiple types were found that match the controller named 'Home'您将不得不为此修复您的默认路由: 发现多种类型与名为“Home”的控制器相匹配

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

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