使用ASP.NET MVC 5的Angular html5mode

时间:2016-07-19 07:20:05

标签: asp.net angularjs asp.net-mvc

在我的项目中,我创建了一个Areas项目Admin。

当html5mode为false时,url localhost:xxxx / Admin运行良好。
但是当html5mode为true时,它无法正常工作并重定向到home。

$locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });

的web.config

<rewrite>
            <rules>
                <rule name="angularjs routes" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/" />
                </rule>
            </rules>
        </rewrite>

HomeController.cs

using System.Web.Mvc;

namespace SuperPaint.Areas.Admin.Controllers
{
    public class HomeController : Controller
    {
        // GET: Admin/Home
        public ActionResult Index()
        {
            return View();
        }
    }
}

AdminAreaRegistration.cs

using System.Web.Mvc;
using System.Web.Optimization;

namespace SuperPaint.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName => "Admin";

        public override void RegisterArea(AreaRegistrationContext context)
        {
            RegisterRoutes(context);
            RegisterBundles();
        }

        private void RegisterRoutes(AreaRegistrationContext context)
        {
            RouteConfig.RegisterRoutes(context);
        }

        private void RegisterBundles()
        {
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

RouteConfig.cs

using System.Web.Mvc;

namespace Project.Areas.Admin
{
    internal static class RouteConfig
    {
        internal static void RegisterRoutes(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", controller = "Home", id = UrlParameter.Optional },
                new[] { "Project.Areas.Admin.Controllers" }
            );
        }
    }
}

1 个答案:

答案 0 :(得分:0)

将您的web.config规则更改为:

  <rules>
            <rule name="IgnoreAdminURLs" stopProcessing="true">
               <match  url ="(admin\/)" ignoreCase="true" />
                <action type="None" />
            </rule>
            <rule name="angularjs routes" stopProcessing="true">
                <match url=".*" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
                </conditions>
                <action type="Rewrite" url="/" />
            </rule>
</rules>