问题描述
我有我建立一个博客系统,我似乎无法获得ASP.NET MVC理解我的路线。
我需要的路由/博客/学生/姓,姓氏,以便/博客/学生/约翰 - 母鹿,它的路线到博客区,学生控制器的指数操作,它需要一个字符串名称参数。
下面是我的路线
routes.MapRoute(
名称:StudentBlogs
网址:博客/学生/(名称),
默认:新{控制器=学生,行动=索引}
);
我的控制器动作
公众的ActionResult指数(字符串名称)
{
字符串[] = nameparts name.Split(新的char [] {' - '});
串的firstName = nameparts [0];
串的lastName = nameparts [1]; 如果(nameparts.Length == 2及和放大器;名称!= NULL)
{
//负荷学生数据库博客
}
返回RedirectToAction(指数,索引,新{面积=博客});
}
但它似乎不会解决...它正常工作与/博客/学生/?的名字=名字,姓氏,但不能使用我想要的路线,这是/博客/学生/姓,姓氏。关于如何解决此问题的任何建议将不胜AP preciated。
我RouteConfig
公共类RouteConfig
{
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO); routes.MapRoute(
名称:StudentBlogs
网址:博客/学生/(名称),
默认:新{控制器=学生,行动=索引},
限制:新{名= @[A-ZA-Z - ] +},
命名空间:新的String [] {IAUCollege.Areas.Blogs.Controllers}
); routes.MapRoute(
名称:网站地图
网址:sitemap.xml的
默认:新{控制器=XmlSiteMap,行动=索引,页= 0}
); // CmsRoute移动到Gloabal.asax //校园地图路线
routes.MapRoute(
名称:CampusMaps
网址:地点/ campusmaps
默认:新{控制器=CampusMaps,行动=索引,ID = UrlParameter.Optional}
);
//核心路线
routes.MapRoute(
名称:默认,
网址:{控制器} / {行动} / {ID}
默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
); //错误路线
routes.MapRoute(
名称:错误
网址:错误/ {}状态,
默认:新{控制器=错误,行动=Error404,状态= UrlParameter.Optional}
);
//添加我们的路线登记MvcSiteMapProvider网站地图
MvcSiteMapProvider.Web.Mvc.XmlSiteMapController.RegisterRoutes(路线);
}
}
您有缺省路由之前声明的自定义路线。否则将被映射{控制器} / {行动} / {ID}
典型的Global.asax如下:
保护无效的Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
如果你创建了一个名为博客一个区域,有相应的BlogsAreaRegistration.cs文件看起来像这样:
公共类BlogsAreaRegistration:AreaRegistration
{
公众覆盖字符串AREANAME
{
得到
{
返回博客;
}
} 公共覆盖无效RegisterArea(AreaRegistrationContext上下文)
{
context.MapRoute(
Admin_default
博客/ {控制器} / {行动} / {ID}
新{行动=索引,ID = UrlParameter.Optional}
);
}
}
连字符有时会像对待向前路线斜杠。当您使用的路线博客/生/约翰 - 美国能源部
,我的猜测是,它是符合上述使用博客/生/约翰区域格局/ DOE
,这将导致404自定义路由添加到默认路由上面BlogsAreaRegistration.cs文件。
I have a blogging system that I'm building and I can't seem to get ASP.NET MVC to understand my route.
the route I need is /blogs/student/firstname-lastname so /blogs/student/john-doe, which routes to a blogs area, student controller's index action, which takes a string name parameter.
Here is my route
routes.MapRoute(
name: "StudentBlogs",
url: "blogs/student/{name}",
defaults: new { controller = "Student", action="Index"}
);
My controller action
public ActionResult Index(string name)
{
string[] nameparts = name.Split(new char[]{'-'});
string firstName = nameparts[0];
string lastName = nameparts[1];
if (nameparts.Length == 2 && name != null)
{
// load students blog from database
}
return RedirectToAction("Index", "Index", new { area = "Blogs" });
}
But it won't seem to resolve...it works fine with /blogs/student/?name=firstname-lastname, but not using the route I want, which is /blogs/student/firstname-lastname. Any advice on how to fix this would be greatly appreciated.
My RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "StudentBlogs",
url: "blogs/student/{name}",
defaults: new { controller = "Student", action = "Index"},
constraints: new { name = @"[a-zA-Z-]+" },
namespaces: new string[] { "IAUCollege.Areas.Blogs.Controllers" }
);
routes.MapRoute(
name: "Sitemap",
url :"sitemap.xml",
defaults: new { controller = "XmlSiteMap", action = "Index", page = 0}
);
//CmsRoute is moved to Gloabal.asax
// campus maps route
routes.MapRoute(
name: "CampusMaps",
url: "locations/campusmaps",
defaults: new { controller = "CampusMaps", action = "Index", id = UrlParameter.Optional }
);
// core route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// error routes
routes.MapRoute(
name: "Error",
url: "Error/{status}",
defaults: new { controller = "Error", action = "Error404", status = UrlParameter.Optional }
);
// Add our route registration for MvcSiteMapProvider sitemaps
MvcSiteMapProvider.Web.Mvc.XmlSiteMapController.RegisterRoutes(routes);
}
}
You have to declare custom routes before the default routes. Otherwise it will be mapping to {controller}/{action}/{id}.
Global.asax typically looks like this:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
If you created an Area named Blogs, there is a corresponding BlogsAreaRegistration.cs file that looks like this:
public class BlogsAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Blogs";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Blogs/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
Hyphens are sometimes treated like forward slashes in routes. When you are using the route blogs/students/john-doe
, my guess is that it is matching the Area pattern above using blogs/students/john/doe
, which would result in a 404. Add your custom route to the BlogsAreaRegistration.cs file above the default routes.
这篇关于如何查询字符串参数转换为路径在asp.net mvc的4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!