扩展Azure移动服务:添加MVC和新路由 [英] Extend Azure Mobile Service: add MVC and new routes

查看:48
本文介绍了扩展Azure移动服务:添加MVC和新路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的azure移动服务运行了一段时间,现在我想为其添加一个网站,这样我就可以在azure云中的一台计算机上同时托管网站和后端服务.我添加了MVC和所有相关的脚手架,例如控制器,视图,并注册了一条路线.我遇到的问题是,我添加的"/Home/Index"新路由以某种方式被重新路由到启动azure Web服务页面

I had my azure mobile service running for a bit, and now I want to add a website to it, so I can host both a website and backend service from one machine in the azure cloud. I have added MVC and all relevant scaffolding like controller, view, and registered a route. The problem I'm having is that the new route that I added "/Home/Index" somehow is getting rerouted to startup azure web service page

这些是我添加的代码中的部分. 控制器:

These are the parts in the code that I've added. Controller:

[RoutePrefix("Home")]
public class HomeController : Controller
{

    [Route("Index")]
    public ActionResult Index()
    {
        return View();
    }
}

查看:

路线:

config.Routes.MapHttpRoute(
            name: "Default",
            routeTemplate: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = RouteParameter.Optional }
        );

我感觉到azure移动服务中添加的路由有些混乱,从而将其重新路由到默认页面.

I feel like there is some mix up with the routes that are being added in the azure mobile service that is rerouting it to default page.

更新 使用指南转移到移动应用服务后,我不得不将所有配置移至Startup.cs:

UPDATE After moving to mobile app service using the guide I had to move all config into Startup.cs:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();
        new MobileAppConfiguration()
            .AddTables(
            new MobileAppTableConfiguration()
                .MapTableControllers()
                .AddEntityFramework())
            .MapApiControllers()
            .AddPushNotifications()
            .ApplyTo(config);

        var builder = new ContainerBuilder();
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

        config.Routes.MapHttpRoute(
            name: "default",
            routeTemplate: "{controller}/{action}",
            defaults: new { controller = "Home", action = "Index" }
        );

        app.UseWebApi(config);
    }
}

因此,在完成所有操作之后,我仍然看到默认页面,并且要路由主页/索引"仍显示此默认页面(如上所示).我删除了AddMobileAppHomeController(),但没有帮助.

So after doing all that I still see the default page and going to route "Home/Index" still shows this default page(showed above). I removed the AddMobileAppHomeController() but it didn't help.

推荐答案

在您的Startup.MobileApp.cs文件中,您具有以下代码:

In your Startup.MobileApp.cs file, you have this code:

new MobileAppConfiguration()
    .UseDefaultConfiguration()
    .ApplyTo(config);

UseDefaultConfiguration()扩展为以下内容:

.AddTables(
    new MobileAppTableConfiguration()
        .MapTableControllers()
        .AddEntityFramework())
    .MapApiControllers()
    .AddMobileAppHomeController()
    .AddPushNotifications()

如果您按照说明扩展呼叫,但将呼叫移至AddMobileAppHomeController(),则您的MVC应用程序将正常工作(假设其余MVC内容实际上按预期路由).

If you expand the call out as described, but remove the call to AddMobileAppHomeController(), your MVC app will work (assuming the rest of your MVC stuff actually routes as expected).

这篇关于扩展Azure移动服务:添加MVC和新路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆