Asp.net mvc 4 MVC 4路由,带UrlParameter。可选参数
在我的MVC解决方案中,我有不同的领域。下面显示了该区域的注册类别之一Asp.net mvc 4 MVC 4路由,带UrlParameter。可选参数,asp.net-mvc-4,asp.net-mvc-routing,Asp.net Mvc 4,Asp.net Mvc Routing,在我的MVC解决方案中,我有不同的领域。下面显示了该区域的注册类别之一 public class CommercialAreaRegistration : AreaRegistration { public override string AreaName { get { return "Commercial"; } }
public class CommercialAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Commercial";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Commercial_default",
"Commercial/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
基于此urlhxxp://localhost:63363/Commercial/VesselManagement 应调用VesselManagement控制器的索引操作方法。它确实偶尔会像预期的那样打电话。但是现在它不执行action方法
但是如果我键入Url作为hxxp://localhost:63363/Commercial/VesselManagement/index/abc 调用Action方法索引并传递参数abs
在这个模式中,不仅要调用这个动作方法,还要调用整个应用程序中的所有动作方法。可能是什么问题。提前感谢大家的帮助
注意:我使用了hxxp而不是http
全球证券交易所
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
//Configure FV to use StructureMap
var factory = new StructureMapValidatorFactory();
//Tell MVC to use FV for validation
ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(factory));
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
}
}
VesselManagement Index()操作
注意:刚才我注意到索引没有任何参数,但我知道这不会影响路由 我真的很抱歉,这个问题是由于我的一位同事的一个错误造成的,他创建了一个区域,对于区域注册,他错误地提到了路由规则
public class SharedAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Shared";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Shared_default",
"{controller}/{action}/{id}", //<---- This made the issue with routing
new { action = "Index", id = UrlParameter.Optional }
);
}
}
公共类共享数据注册:区域注册
{
公共重写字符串区域名
{
得到
{
返回“共享”;
}
}
公共覆盖无效注册表区域(AreaRegistrationContext上下文)
{
context.MapRoute(
“共享默认值”,
“{controller}/{action}/{id}”,//您可以显示其他路由吗,例如全局上下文中的路由?我猜是另一个路由正在拾取它,但如果没有更多上下文,我无法确定。VesselManagement
的签名是什么?id
参数是可为空的类型吗?当hxxp://localhost:63363/Commercial/VesselManagement
被调用找不到资源。HTTP 404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下URL并确保其拼写正确。
public class SharedAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Shared";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Shared_default",
"{controller}/{action}/{id}", //<---- This made the issue with routing
new { action = "Index", id = UrlParameter.Optional }
);
}
}
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
// Log the exception.
//ILogger logger = Container.Resolve<ILogger>();
// logger.Error(exception);
Response.Clear();
HttpException httpException = exception as HttpException;
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
if (httpException == null)
{
routeData.Values.Add("action", "Index");
}
else //It's an Http Exception, Let's handle it.
{
switch (httpException.GetHttpCode())
{
case 404:
// Page not found.
routeData.Values.Add("action", "HttpError404");
break;
case 500:
// Server error.
routeData.Values.Add("action", "HttpError500");
break;
// Here you can handle Views to other error codes.
// I choose a General error template
default:
routeData.Values.Add("action", "General");
break;
}
}
// Pass exception details to the target error View.
routeData.Values.Add("error", exception);
// Clear the error on server.
Server.ClearError();
// Avoid IIS7 getting in the middle
Response.TrySkipIisCustomErrors = true;
// Call target Controller and pass the routeData.
//IController errorController = new ErrorController();
//errorController.Execute(new RequestContext(
// new HttpContextWrapper(Context), routeData));
}