我用ASP.NET vnext创建了一个非常小的项目,它带有一个简单的MVC控制器和一个简单的Api控制器。
在localhost上,一切正常。 如果我部署到azure网站,只有MVC部分正在运行
这是我的project.json
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"exclude": [
"wwwroot"
],
"packExclude": [
"node_modules",
"bower_components",
"**.kproj",
"**.user",
"**.vspscc"
],
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-beta2",
"Microsoft.AspNet.Mvc": "6.0.0-beta2"
},
"frameworks" : {
"aspnet50" : { },
"aspnetcore50" : { }
}
}
Startup.cs
using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
namespace Project.Web6
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
app.UseMvc();
}
}
}
MVC控制器
public class HomeController : Controller
{
// GET: /<controller>/
public string Index()
{
return "Hello world";
}
}
API控制器
[Route("api/[controller]")]
public class MessagesController : Controller
{
// GET: api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
MVC控制器工作正常,但如果我转到http://myproject.azurewebsites.net/api/Messages则返回404
答案 0 :(得分:2)
以下是我解决问题的方法:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
我不知道为什么它在当地工作......