Extend Azure Mobile Service: add MVC and new routes

岁酱吖の 提交于 2019-12-12 02:38:46

问题


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();
    }
}

View:

Route:

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

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.

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);
    }
}

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.


回答1:


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

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

The UseDefaultConfiguration() expands out to the following:

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

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).



来源:https://stackoverflow.com/questions/40581893/extend-azure-mobile-service-add-mvc-and-new-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!