我看到了asp.net-mvc 2中区域的新功能.它让我思考.我为什么需要这个?我对用例进行了一些阅读,并且在一个特定的点上,我的控制器应该有多大和多大的范围?
我应该尝试拥有许多小控制器吗?一个大控制器?
人们如何确定控制器数量的最佳位置?我认为我的可能太大了(首先我有问题区域,因为我的控制器名称应该是一个区域并且有一些较小的控制器)
我在一个区域有一条自定义路线如下:
context.Routes.Add(
"SearchIndex - By Location - USA",
new CountryTypeSpecificRoute(
CountryType.UnitedStates,
"search/{locationType}-in-{query}",
new { controller = "Search", action = "Index", query = UrlParameter.Optional },
new { locationType = new UsaLocationSearchRouteConstraint() })
);
Run Code Online (Sandbox Code Playgroud)
示例网址:
/搜索/社区功能于新约克城
解决行动很好.但它无法找到View.
未找到视图"索引"或其主数据或视图引擎不支持搜索的位置.搜索了以下位置:〜/ Views/Search/Index.cshtml~/Views/Shared/Index.cshtml
该视图位于〜/ Areas/Search/Views/Search/Index.cshtml中
为什么不看那里?
如果我这样做context.MapRoute而不是context.Routes.Add,那就有效.所以它似乎与我使用自定义路线的事实有关?
有任何想法吗?
asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas asp.net-mvc-3
我的模特是
public class SiteConfig
{
public SiteConfig()
{
}
public int IdSiteConfig { get; set; }
public string Name { get; set; }
public byte[] SiteLogo { get; set; }
public string Brands { get; set; }
public string LinkColour { get; set; }
public IEnumerable<SiteBrand> SiteBrands { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public class SiteBrand
{
public int Id { get; set; }
public int SiteId { get; set; }
public int BrandId { get; set; }
public Brand …Run Code Online (Sandbox Code Playgroud) asp.net-mvc-areas c#-4.0 asp.net-mvc-3 ef-database-first asp.net-mvc-4
我有一个巨大的MVC应用程序但不幸的是MVC领域没有用于初始开发.应用程序变得如此模块化,我们觉得有一个很好的案例可以使用区域.将整个代码移动到"区域"结构是多么容易或困难..是否有任何重大挑战/代码重新工作等我必须注意的事情?我可以创建新区域并将控制器视图模型移动到适当的存储桶中......它工作正常吗?我是在简化事情吗?如果有人之前采取过这条路线,会很感激反馈,评论..
我希望网站的默认页面是Login.cshtml.我得到了例外:
错误:未找到视图"LogIn"或其主节点,或者没有视图引擎支持搜索的位置.搜索了以下位置:
我有2个区域.结构如下所示.

我的routeconfig如下所示.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Portal.Web
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "LogIn", id = UrlParameter.Optional },
namespaces: new[] { "Portal.Web.Areas.Management" }
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的global.asax.cs如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
namespace Portal.Web
{
// Note: For instructions on …Run Code Online (Sandbox Code Playgroud) 我正在使用asp.net mvc 3和.net framework 4.0与C#
假设我有NivoSlider(滑块)html代码.它还有js,css和图像文件.
我想使用NivoSlider cshtml代码和js/css/images作为"项目",我想将它添加到不同的MVC解决方案中.它将成为一个插件.
我无法将其视为部分视图,因为我必须将所有css,js和imges文件移动到新的解决方案中.我看了"mvc区域",但它没有渲染我的js和css文件(事实上;使用"区域"并不是一个完美的方式,因为你知道)...我查找了一些基于插件的架构,但没有 - 他们很容易实现.我的时间有限.
那么我该如何解决这个问题呢?
编辑:我可以使用这个http://razorgenerator.codeplex.com/ - 但如果你提出一个想法,我正在寻找另一种解决方案......
我的MVC 5应用程序中有两个区域无法正常工作。
当我使用以下链接时http://localhost:45970/Admin/Admin,应用会加载正确的index.cshtml whicxh,/Areas/Admin/Views/Admin/Index.cshtml但是当我尝试加载时,http://localhost:45970/Admin它会尝试从加载Index.cshtml文件/Views/Admin/Index.cshtml。
所有搜索结果都表明我在做正确的事。我什至已经加载了一个示例API项目,以查看其中的帮助区域,以确保我做的正确。
这是我的RouteConfig.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace BlocqueStore_Web
{
public class RouteConfig
{
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[] { "BlocqueStore_Web.Controllers" }
);
}
}
}
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);
}
Run Code Online (Sandbox Code Playgroud)
最后是我的AdminAreaRegistration.cs文件
using …Run Code Online (Sandbox Code Playgroud) 我目前遇到一个问题,我在同一个解决方案 Visual Studio 2017 中有一个 mvc 项目和一个 API 项目。当我构建解决方案时,结构如下解决方案解决方案/区域1解决方案/区域2解决方案/api我能够仅在本地计算机上构建时,可以毫无问题地访问 api,但是当我发布解决方案时,api 项目会构建 dll,但不会构建解决方案/api,因此我无法导航到我的 api 调用。我尝试设置多个启动解决方案,但没有解决问题。
我想一定路线地图,以便自动生成的URL看起来
Admin/controller/action/param对于这两个代码块,
@Url.Action("action","controller",new{id="param"})并
@Url.Action("action","controller",new{type="param"})
我所做的是在区域注册中的以下内容,
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index",
id = UrlParameter.Optional },
new string[] { "namespaces" });
context.MapRoute(
"Admin_type",
"Admin/{controller}/{action}/{type}",
new { action = "Index",
type = UrlParameter.Optional },
new string[] { "namespaces" });
Run Code Online (Sandbox Code Playgroud)
当参数名称是id,生成的url是预期的,但是当参数名称是type,而不是controller/action/typevalue,它生成类似的东西controller/action/?type=typevalue
有没有办法生成url类似于controller/action/typevalue保持Admin_default路由器的生成器行为完好无损?
asp.net-mvc asp.net-mvc-routing asp.net-mvc-areas asp.net-mvc-3
我正在玩它们内部的区域和路由.我想要实现的是能够拥有一个看起来像这样的URL;
PracticeAdmin/Practice/[Practice Name]
Run Code Online (Sandbox Code Playgroud)
然后,我可以添加编辑和删除等内容.
过去通过在操作中添加此注释来处理区域时,我已经实现了这一点
[Route("PracticeAdmin/Practices/{practiceName}")]
public ActionResult Details(string practiceName)
Run Code Online (Sandbox Code Playgroud)
这会生成我想要的URL.我遇到的问题是,当我在使用区域时尝试这样做时,我得到的链接看起来像这样;
PracticeAdmin/Practices?practiceName=Practice1
Run Code Online (Sandbox Code Playgroud)
这不是我要找的.
我用来尝试生成这个的代码是
using System.Web.Mvc;
namespace TrainingPortal.Areas.PracticeAdmin
{
public class PracticeAdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "PracticeAdmin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"PracticeAdmin_default",
"PracticeAdmin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "TrainingPortal.Areas.PracticeAdmin.Controllers" }
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
namespace TrainingPortal
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute( …Run Code Online (Sandbox Code Playgroud)