我想为具有正常CRUD操作的控制器设置路由,但希望详细信息操作不在URL中显示"详细信息".Stackoverflow似乎配置了这种类型的路由:
http://stackoverflow.com/questions/999999/
http://stackoverflow.com/questions/ask
Run Code Online (Sandbox Code Playgroud)
使用这个类比,我的路线目前看起来像:
http://stackoverflow.com/questions/Details/999999/
Run Code Online (Sandbox Code Playgroud)
通过添加以下路线,我可以Details删除:
routes.MapRoute("Q1", "questions/{id}",
new { controller = "Questions", action = "Details" });
Run Code Online (Sandbox Code Playgroud)
但是,在控制器上启动其他操作(例如,/questions/new对于此示例)则抱怨无法解析id.
有没有办法设置路由,以便我不必手动输入所有其他操作(MapRoute"items/create","items/delete"等)到Global.asax.cs?我基本上想要第二条路线:
routes.MapRoute("Q2", "questions/{action}",
new { controller = "Questions", action = "Index" });
Run Code Online (Sandbox Code Playgroud)
...并且如果{id}匹配整数,并且{action}如果它是字符串,则让路由引擎使用路由Q1 .这可能吗?
如何在global.asax中定义路由,以便能够使用可为空的参数和逗号作为分隔符?
我正在尝试为我的搜索用户页面实施路由规则
"{Controller}/{Action},{name},{page},{status}"
Run Code Online (Sandbox Code Playgroud)
来自Global.asax的完整条目:
routes.MapRoute(
"Search",
"{controller}/{action},{name},{page},{status}",
new { controller = "User", action = "Find",
name = UrlParameter.Optional,
page = UrlParameter.Optional,
status = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
当我输入所有参数时,如上定义的例程工作正常,但是当某些参数等于null时路由失败(例如“ user / find ,,”)
根据Clicktricity的注释,下面是处理请求的动作方法的唯一性:
public ActionResult Find(string userName, int? page, int? status)
{
// [...] some actions to handle the request
}
Run Code Online (Sandbox Code Playgroud)
一开始,我是通过VS调试器测试路由的,现在我使用的是Phil的Haack博客上介绍的路由调试器。该工具确认-没有正确处理具有空值的路由(或者我做错了;))
我在这里做的很糟糕.我问的是一个问题,没有先尝试跳跃,有人知道一个简单的方法.
我们有机会使ASP.NET MVC路由系统区分大小写吗?我希望以下两个网址不同:
example.com/a
example.com/A
我们有一个简单的解决方案或应该为此编写我们自己的处理程序.
我有一个有两个模型的网络项目 - IndicatorModel和GranteeModel.我也有相应的ApiControllers - IndicatorsController,和GranteesController.我打算将这个设置用于我的实际web项目的数据API,所以我在我的项目中创建了一个名为"Api"的新区域.在我的ApiAreaRegistration班上,我正在为这些控制器注册路由,如下所示:
context.Routes.MapHttpRoute(
name: "ApiDefault",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
基本上,http://myapp/api/indicators/123应该转到Indicators控制器的请求,它应该特别由接受整数参数的action方法处理.我的控制器类设置如下,它完美地工作:
public class IndicatorsController : ApiController
{
// get: /api/indicators/{id}
public IndicatorModel Get(int id)
{
Indicator indicator = ...// find indicator by id
if (indicator == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return new IndicatorModel(indicator);
}
}
Run Code Online (Sandbox Code Playgroud)
我的GranteesController课程设置相同:
public class GranteesController : ApiController
{
// get: /api/grantees/{id}
public GranteeModel Get(int …Run Code Online (Sandbox Code Playgroud) 我有一个带索引动作的控制器.
public ActionResult Index(int id = 0)
{
return view();
}
Run Code Online (Sandbox Code Playgroud)
我希望将id传递给索引操作,但它似乎与detail操作的工作方式不同.
例如,如果我想将id 4传递给索引动作,我必须访问url:
http://localhost:8765/ControllerName/?id=4
Run Code Online (Sandbox Code Playgroud)
详情动作......我可以做到这一点.
http://localhost:8765/ControllerName/Details/4
Run Code Online (Sandbox Code Playgroud)
我想用Index做什么就像......
http://localhost:8765/ControllerName/4
Run Code Online (Sandbox Code Playgroud)
当我访问此网址时,出现错误:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /fix/1
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929 …Run Code Online (Sandbox Code Playgroud) 我的目标是从它的名称和区域中找到一个控制器.如果我的当前httpContext区域与待定控制器位于同一区域内,我已成功完成此操作.但是,我无法接受ControllerFactory考虑区域的呼吁.这是我的代码:
public static ControllerBase GetControllerByName(this HtmlHelper htmlHelper, string controllerName)
{
IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
IController controller = factory.CreateController(htmlHelper.ViewContext.RequestContext, controllerName);
if (controller == null)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "The IControllerFactory '{0}' did not return a controller for the name '{1}'.", factory.GetType(), controllerName));
}
return (ControllerBase)controller;
}
Run Code Online (Sandbox Code Playgroud)
因为它RequestContext是一个参数,我为它添加了一个"区域"的路由值,但没有变化.我可以用requestContext做些什么来考虑区域吗?我是否需要覆盖控制器工厂 - 如果是这样,特别是处理区域区别的是什么?
更新:
以下是我所拥有的AreaRegistration示例:
public class StoresAreaRegistration : AreaRegistration
{
public override string AreaName { get { return "Stores"; } }
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute( …Run Code Online (Sandbox Code Playgroud) 我确实看到了类似问题的答案,但没有一个适用于这种情况.
我有一个名为"设置"的注册区域的MVC4应用程序,带有一个"管理"控制器.我的网络项目中还有一个名为"设置"的源文件夹,因此文件夹结构如下:
+ WebProjectFolder
+ Areas
+Settings
+Controllers
ManageController.cs
+ SettingsAreaRegistration.cs
...
+ Settings
+ SomeClasses.cs
Run Code Online (Sandbox Code Playgroud)
ManageController非常简单:
public class ManageController : Controller
{
public ActionResult Index()
{
return Content("WORKS FINE!");
}
}
Run Code Online (Sandbox Code Playgroud)
区域注册也很简单:
public class SettingsAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Settings";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.IgnoreRoute("Settings/{*pathinfo}");
context.Routes.RouteExistingFiles = false;
context.MapRoute(
"Settings_default",
"Settings/{controller}/{action}/{id}",
new {area = context.AreaName, controller="Manage", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
http://mysite/settings …Run Code Online (Sandbox Code Playgroud) 我们正在将一个旧的asp.net网站重新编写成MVC4.
我们的网站有许多链接看起来像这样(我们无法控制但必须支持):
www.some.com/page.aspx?id=5
Run Code Online (Sandbox Code Playgroud)
有没有办法得到/page.aspx?id=5的请求到路由,以便我们可以处理请求,将其传递给控制器/操作,然后从那里处理它?
我在控制器级别应用了一个路由属性,但是我希望将一个动作排除在路由之外.没有覆盖,但完全排除了路线.怎么能实现这一目标?
比方说我有:
[RoutePrefix("promotions")]
[Route("{action=index}")]
public class ReviewsController : Controller
{
// eg.: /promotions
public ActionResult Index() { ... }
// eg.: /promotions/archive
public ActionResult Archive() { ... }
// eg.: /promotions/new
public ActionResult New() { ... }
// eg.: /promotions/edit/5
[Route("edit/{promoId:int}")]
public ActionResult Edit(int promoId) { ... }
public void Internal() { ... }
}
Run Code Online (Sandbox Code Playgroud)
我希望内部不要被路由.
我原本期望找到[DoNotRoute]或[Ignore]属性,但我没有找到类似的东西.
所以我创建了自己的ControllerFactory,并且我正在重载GetControllerSessionBehavior以扩展MVC行为.
要做我的自定义工作,我必须对被调用的动作使用反射.然而,我偶然发现了一个奇怪的问题 - 我无法通过访问RequestContext.RouteData来检索操作
在为此设置复制样本时,我无法重现错误.
是否有人知道可能的原因或知道如何通过调用除此之外的请求上下文的方法来检索操作?
public class CustomControllerFactory : DefaultControllerFactory
{
protected override SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, Type controllerType)
{
if (!requestContext.RouteData.Values.ContainsKey("action"))
return base.GetControllerSessionBehavior(requestContext, controllerType);
var controllerAction = requestContext.RouteData.Values["action"];
var action = controllerAction.ToString();
var actionMethod = controllerType.GetMember(action, MemberTypes.Method, BindingFlags.Instance | BindingFlags.Public).FirstOrDefault();
if(actionMethod == null)
return base.GetControllerSessionBehavior(requestContext, controllerType);
var cattr = actionMethod.GetCustomAttribute<SessionStateActionAttribute>();
if (cattr != null)
return cattr.Behavior;
return base.GetControllerSessionBehavior(requestContext, controllerType);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以调用的操作很好,但无法访问我的控制器工厂中的操作名称:
[Route("Open/{createModel:bool?}/{tlpId:int}/{siteId:int?}")]
public ActionResult Open(int tlpId, int? siteId, bool? createModel = true)
{
}
Run Code Online (Sandbox Code Playgroud)
欢迎任何想法.
更新:
问题似乎与属性路由有关.虽然它在repro中运行良好但它对我来说不适用于生产.
途中发现这一点-一旦 …