我尝试向我的 .NET Core 项目添加一个区域,但我总是看到该错误:
RouteCreationException:创建名称为“(我的区域名称)”的路线时出错
我的代码是:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "custom",
template: "{area:my area name}/{{controller=AdminHome}/{action=Index}/{id?}");
});
}
Run Code Online (Sandbox Code Playgroud)
在配置服务中,我添加了以下代码:
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddRouting();
//...
}
Run Code Online (Sandbox Code Playgroud)
在控制器中我添加了:
[Area("My Area Name")]
public class AdminHomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
RouteCreationException:创建名称为“custom”且模板为“{area:area name}/{{controller=Home}/{action=Index}/{id?}”的路由时出错。\r\n Microsoft.AspNetCore.Routing.RouteBase..ctor(string template, string name, IInlineConstraintResolver constraintResolver, …
我在MVC 3项目中添加了一个区域.我似乎无法通过一个非常简单的场景来处理路由.它似乎总是希望解决该地区.这是我的配置.在启动时:
AreaRegistration.RegisterAllAreas();
IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Browse", action = "Index", id = UrlParameter.Optional }
Run Code Online (Sandbox Code Playgroud)
和
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get { return "Admin"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Users", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
在web.config中:
<authentication mode="Forms">
<forms loginUrl="~/Login" defaultUrl="~/Browse" timeout="60" cookieless="UseDeviceProfile" />
</authentication>
Run Code Online (Sandbox Code Playgroud)
我正在使用RouteDebugger来尝试解决它.当我导航到Login页面时,调试器显示:
到现在为止还挺好.但它显示了这一点: …
可能重复:
如何注册已在AREA中创建的控制器
我有一个问题 - 是否可以做下一个?
我有三个方面:
_Default
SiteOne
SiteTwo
Run Code Online (Sandbox Code Playgroud)
在每个区域内我都有一个名称相同的ApiController,但当然在不同的名称空间中:
MvcAppliaction.Areas._Default.Controllers.ValuesController
MvcAppliaction.Areas.SiteOne.Controllers.ValuesController
MvcAppliaction.Areas.SiteTwo.Controllers.ValuesController
Run Code Online (Sandbox Code Playgroud)
我还有一个当前值(我想使用)的配置区域.
如果他进入浏览器,我想将用户映射到适当区域(我可以在配置中找到)中的控制器:
/api/values
Run Code Online (Sandbox Code Playgroud)
例如,如果配置文件中的当前区域是SiteOne,则此请求应映射到MvcAppliaction.Areas.SiteOne.Controllers.ValuesController控制器,但如果我将配置文件中的当前区域更改为_Default的SiteTwo,则应将其映射到正确的控制器.
PS.使用MVC控制器很简单,您只需设置路线:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MvcApplication.Web.Areas." + SiteName + ".Controllers") }
);
Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc-areas asp.net-web-api asp.net-web-api-routing
我有一个ASP.net MVC应用程序已经生产了一段时间.我想通过创建一个新的区域向我想要做的应用程序添加一个新的Admin部分.我只是好奇在添加这个新区域时我可能需要注意哪些事情.
特别:
我正在构建一个模块化的MVC4应用程序,其中每个模块(= area)都是一个类库.模型和控制器编译成.dll,视图被复制到相应的文件夹中.在运行时,一切正常.在设计时,还有一个恼人的问题:在类库中编辑剃刀视图时,Visual Studio无法识别System.Web.Optimization命名空间.
The name "Styles" does not exist in the current context.
The name "Scripts" does not exist in the current context.
Run Code Online (Sandbox Code Playgroud)
我尝试将程序集添加到root和内部web.config中的system.web/compilation部分:
<add assembly="System.Web.Optimization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
Run Code Online (Sandbox Code Playgroud)
我没有特定的版本也尝试过它.这两种方法都没有解决问题,但是触发了asp.net运行时错误(在剃刀视图的第一行可见):
Could not load file or assembly 'System.Web.Optimization, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
Run Code Online (Sandbox Code Playgroud)
程序集在项目中引用,"Copy Local"设置为"True".它还被添加为剃刀配置部分中的命名空间.
我怀疑这是我将来遇到的其他组件的一般问题.
编辑:我做了一般设置,让Intellisense进入类库中的剃刀视图.到目前为止,一切都有效,除了VS2010无法识别优化命名空间.
asp.net-mvc asp.net-mvc-areas razor asp.net-mvc-4 system.web.optimization
ASP.NET MVC中的区域可以方便地将站点分成比控制器更高级别的更小,可管理的组件.它们就像是Web应用程序中的迷你MVC片段.
在ASP.NET区域的任何Java MVC框架中是否存在等效概念?
如果没有,在Java MVC框架中模拟其功能时是否有关于最佳实践的建议?
我在网上部署了一个mvc3应用程序.如何在Url.Action()方法中添加虚拟目录名称?
例如:我的应用程序在mydomain.com\app中
现在当我这样做
Url.Action返回action ="/ Home/Create",但我想要的是action ="/ app/Home/Create".
应该做什么 ?
我的Razor Layout视图中有以下代码(由我的应用程序中的所有视图共享):
@using (Html.BeginForm("Logout", "Account", FormMethod.Post, new { id = ViewIDs.Shared._AuthenticationPartial.LogoutForm })) {
Run Code Online (Sandbox Code Playgroud)
这与我的家庭和帐户视图一起工作正常,即它呈现了一个发布到〜/ Account/Logout的表单.但是,当与名为"Person"的区域内的视图一起使用时,它会突然发布到〜/ Person/Account/Logout.
现在,我能够解决这个问题如下:
@using (Html.BeginForm("Logout", "Account", new { area = "" }, FormMethod.Post, new { id = ViewIDs.Shared._AuthenticationPartial.LogoutForm })) {
Run Code Online (Sandbox Code Playgroud)
这是否是正确的方法,即根据定义当前区域的默认区域?或者我的应用程序中存在配置问题?
我在主/顶部区域有一个Contacts控制器,我有一个名为"Contacts"的区域.
如果我在注册顶级路线之前注册我的区域,我会将POST 404s发送到Contacts控制器:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ModelBinders.Binders.DefaultBinder = new NullStringBinder();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
Run Code Online (Sandbox Code Playgroud)
而且,如果我在路线后注册我的区域,我的404到联系人控制器就会消失,但我到联系人区域的路线现在是404s.
...记录了许多重复的控制器名称问题,但我没有找到该区域与控制器名称相同的特定方案.
...可能很容易解决.很感激帮助.:-D
fwiw,我正在使用显式命名空间注册我的Contacts区域:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyMvcApplication.Controllers" }
);
}
Run Code Online (Sandbox Code Playgroud) 我知道之前已经出现过这个错误,但这似乎有点特殊.
我一直在使用基于ASP.NET MVC 4的ReactJS设置SPA.我在我的机器上运行工作没有问题.但是,我看到的一个奇怪的问题是,它不适用于其他开发人员的任何其他机器.据我所见,我没有任何未在源代码管理下签入的文件.我已经使用了RouteDebugger,我看到了正确的路线被抓住了.
我用于此SPA的路线是/ V2/Home.所以我有一个名为"V2"的区域,一个名为"HomeController"的区域中的MVC控制器,它有一个名为"Index"的视图.我在V2AreaRegistration中设置了一个catchall.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"V2_default",
"V2/{*url}",
new { area = "V2", controller = "Home", action = "Index" }
);
}
Run Code Online (Sandbox Code Playgroud)
这是Global.asax.cs中的Application_Start
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
AutoMapperConfiguration.Configure();
Logger.Info("Application started");
GlobalConfiguration.Configuration.EnsureInitialized();
}
Run Code Online (Sandbox Code Playgroud)
我完全无处可去.我很乐意解决这个问题.随意要求任何遗漏.
asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas asp.net-mvc-4