简体   繁体   English

带有ASP.NET MVC 5的Angular html5mode

[英]Angular html5mode with ASP.NET MVC 5

In my project, I create a Areas project Admin. 在我的项目中,我创建一个Areas项目Admin。

When html5mode is false, url localhost:xxxx/Admin working well. 如果html5mode为false,则url localhost:xxxx / Admin运行良好。
But when html5mode is true, it not working and redirect to home. 但是当html5mode为true时,它将无法正常工作并重定向到home。

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

web.config 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 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 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 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" }
            );
        }
    }
}

Change your web.config rules to: 将您的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>

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

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