路由无法在MVC3中工作 [英] Routing not working in MVC3

查看:80
本文介绍了路由无法在MVC3中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我给出如下路线时。

它正确地重定向到页面。

但是网址没有正确显示。

而不是AppDevelopment / Info,它显示为Home / Info / Apps





 routes.MapRoute(Apps,AppDevelopment / Info,new {controller =Home,action =Info,id =Apps}); 





请帮我解决这个问题

解决方案

看看你的整个注册表会有所帮助路线方法。通过在MVC3中设置路由,您拥有它们的顺序是最重要的。您不应该在默认路由下创建新路由。您创建的任何新路由必须位于默认路由之前...否则它将无法工作。所以这就是为什么我说看到你的路线方法会有所帮助。



以下是我的样本mvc 3项目中的路由方法



我也是想知道为什么你在你的路线上强迫你。如果您从路由强制ID,为什么不能在控制器中硬编码。如果我想看看我的MVC应用程序的应用程序开发信息,那么关闭你的例子...好吧,我不能像在AppDevelopment / Info / MvcApp那样在URL的末尾打它,因为你已经硬编码了路由以便始终使用应用程序的ID ...所以我是否输入AppDevelopment / Info或AppDevelopment / Info / Your grandma ...该路线的ID将始终为Apps。在我看来,这是一个不好的做法。



另外需要注意的是,你正在硬编码路由URL模板路由的动作然后也在默认值中指定Action ...因为您已将其硬编码到URL模板中...您不需要在默认值中指定它。



< pre lang =c#> // RouteConfig.cs中的路由方法
public static void RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute( {resource} .axd / {* pathInfo} );

// 我的新路线高于默认路线
routes.MapRoute(
name: Apps
url: AppDevelopment / Info
默认值: new {controller = Home,action = 信息,id = 应用});

routes.MapRoute(
name: 默认
url: {controller} / {action} / {id}
默认值: new {controller = Home,action = 索引,id = UrlParameter.Optional}
);
}





  //  我的样本中的我家控制器...注意没有为Info路由指定Id ...因为你已经在配置中硬编码到你的路由中。 
public class HomeController:Controller
{
public ActionResult Index()
{
return View();
}

public ActionResult Info()
{
ViewBag.Message = 您的应用程序描述页面。;

return 查看();
}

public ActionResult联系人()
{
ViewBag.Message = 您的联系页面。;

return 查看();
}
}





我认为这是实现这条路线的更好方式你想要什么,但要足够灵活,可以根据你的需要进行扩展。



  //  更好地实施路线以实现灵活性 
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection路由)
{
routes.IgnoreRoute( {resource} .axd / {* pathInfo});

// 示例第二条路线演示如何/在何处添加其他路线
routes.MapRoute(
name: AppsRoute2
url: PersonalDevelopment / {action} / {id}
默认值:< span class =code-keyword> new {controller = Home}) ;

// 请注意,此路线的ID为{appName} ...因为你需要确保他们在控制器的方法(动作)中参数匹配名称
// 。如果他们不匹配...你的路线将无法正常工作。请注意Home / Info控制器上Info的参数名称
//
// 此外,通过在此路线中将行动分为{action} ...你可以允许主控制器
// 响应任何以AppDevelopment / MyCustomHomeController方法开头的请求这里不是仅仅是信息。
//
< span class =code-comment> // 要获得将信息硬编码到路线中的位置...反转两个Url参数的注释(将评论输出未注释的评论并取消注释掉评论)
routes.MapRoute(
name: 应用
ur l: AppDevelopment / {action} / {appName}
// url:AppDevelopment / Info / {appName},
// 默认值:new {controller =Home,action =Info});
默认值: new {controller = Home});

routes.MapRoute(
name: 默认
url: {controller} / {action} / {id}
默认值: new {controller = Home,action = 索引,id = UrlParameter.Optional}
);



}
}





现在为控制器。在上面的代码中注释...注意Info动作中的param名称(方法)。



  public   class  HomeController:Controller 
{
public ActionResult Index()
{
return View();
}

public ActionResult Info( string appName)
{
ViewBag.Message = 您的应用程序说明页面。;

return 查看();
}

public ActionResult联系人()
{
ViewBag.Message = 您的联系页面。;

return 查看();
}
}





示例查看以查看上述代码的每个阶段中的ID。可以使用此代码来查看硬编码路由将覆盖您在第一位代码中传入URL的任何ID,然后还会看到您使用第二个路由示例从URL传入的内容将反映在路线。



<前lang =HTML>使用硬编码的Id路线值查看
< br / >
@ Url.RequestContext.RouteData.Values [id]
< br / > < br / > < br / > < br / > < br / > < br / >
< br / > < br / >
使用Id的路径ID结果已更改为自定义ID并通过viewbag传递的路径
< br / > < br / >
@ ViewBag.Message





所以现在使用我给你的上述代码......以下路由可以运行 http:// localhost:54425 / AppDevelopment / Info / MyMvcApp [ ^ ]



http:// localhost:54425 / AppDevelopment / Info / My%20Second%20App [ ^ ]



您要查找的网址基于你的问题



http:// localhost:54425 / AppDevelopment / Info /应用 [ ^ ]



如果您对上述代码有任何疑问,请告诉我我很乐意帮助您理解


我创建了不同的控制器和操作方法。



然后我在行动方法中使用了redirecttoroute(名字)。



如下:



AppDevelopment Controller:



公共ActionResult信息(字符串ID)

{

返回Redirectt oRoute(Id);

}



谢谢各位朋友,



问候

Lalitha


默认Routeconfig.cs

 routes.MapRoute(
name:默认,
url:{controller} / {action} / {id},
默认值:new {controller =Home,action =Index,id = UrlParameter.Optional}
);



{id}:选项参数

无需从此处修复

查看此MapRoute设置会对页面加载时间产生影响。

默认情况下,id是optionparamter,你正在使用Apps值进行修复,这就是id带来URL的原因


Hi,
when i give the route as follows.
it redirect to the page correctly.
but the url not displaying correctly.
instead of "AppDevelopment/Info",it is displaying as "Home/Info/Apps"


routes.MapRoute("Apps", "AppDevelopment/Info", new { controller = "Home", action = "Info", id = "Apps" });



please help me to resolve this

解决方案

What would be helpful is to see your entire Register Routes method. With setting up routes in MVC3 the order you have them is the most important. You should not have a new route you created below the Default route. Any new routes you create must come before the Default route...otherwise it wont work. So that is why i say seeing your routes method would be helpful.

So below is the routes method that works in my sample mvc 3 project

I would also am wondering why you are forcing an Id in your route. If your forcing an Id from the route why can't you just hard code it in your controller. So going off your example if i wanted to see the app development info for my MVC App...well i cant slap it on the end of the URL like AppDevelopment/Info/MvcApp because you've hard coded the route to always use the Id of "Apps"...so whether i type AppDevelopment/Info or AppDevelopment/Info/Your Grandma...the Id for that route will always be Apps. This is a bad practice in my opinion.

The other thing to note is that you are hard coding the into your route URL template the Action of the route and then also specifying the Action within the defaults...because you've hardcoded it into the URL template...you would not need to specify it in the defaults.

// Routes Method in RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
	routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
	
//My new route is above the Default Route
	routes.MapRoute(
		name: "Apps",
		url: "AppDevelopment/Info",
		defaults: new { controller = "Home", action = "Info", id = "Apps" });

	routes.MapRoute(
		name: "Default",
		url: "{controller}/{action}/{id}",
		defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
	);
}



// My home controller in my sample...notice no Id is specified for the Info route...because you've hardcoded into your route in the config.
public class HomeController : Controller
{
	public ActionResult Index()
	{
		return View();
	}

	public ActionResult Info()
	{
		ViewBag.Message = "Your application description page.";

		return View();
	}

	public ActionResult Contact()
	{
		ViewBag.Message = "Your contact page.";

		return View();
	}
}



What i would consider as the better way of going about and implementing this route to do what you want but be flexible enough to scale based on your need is below.

//Better implementation of the route to allow for flexibility
public class RouteConfig
{
	public static void RegisterRoutes(RouteCollection routes)
	{
		routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

		// Sample second route to demo how/where to add an additional route
		routes.MapRoute(
			name: "AppsRoute2",
			url: "PersonalDevelopment/{action}/{id}",
			defaults: new { controller = "Home" });

		// notice that the Id for this route is {appName}...because of that you will need to make sure that they paramtere within the method (action) of the controller match the name 
		//      used in the route. If they don't match...your route will not work. Say take note on the Home/Info controller the parameter name for Info
		//
		//  Also by sepcifying the action to be {action}  in this route...you can allow the home controller to 
		//      respond to any request that starts with AppDevelopment/MyCustomHomeController Method Here instead of only being Info.
		//
		//  To get it where you've hard coded the Info into the route...reverse the comments on the two Url parameters (make the comment out the uncommented one and uncomment the commented out one)
		routes.MapRoute(
			name: "Apps",
			url: "AppDevelopment/{action}/{appName}",
			//url: "AppDevelopment/Info/{appName}",
			//defaults: new { controller = "Home", action = "Info" });
			defaults: new { controller = "Home"});
		
		routes.MapRoute(
			name: "Default",
			url: "{controller}/{action}/{id}",
			defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
		);

		

	}
}



Now for the controller...like comment in code above...take note of the param name in the Info action (method).

public class HomeController : Controller
{
	public ActionResult Index()
	{
		return View();
	}

	public ActionResult Info(string appName)
	{
		ViewBag.Message = "Your application description page.";

		return View();
	}

	public ActionResult Contact()
	{
		ViewBag.Message = "Your contact page.";

		return View();
	}
}



Sample View to see the Id within each stage of the above code. Can use this code to see that hard coding the route will override any Id you pass in on the URL in first bit of code and then to also see that what you pass in from the URL using the second route example will reflect whatever is on the route.

To view with hardcoded Id route value
<br />
@Url.RequestContext.RouteData.Values["id"]
<br /><br /><br /><br /><br /><br />
<br /><br />
Route Id result using the Id of the route that was changed to a custom Id and passed in over the viewbag
<br /><br />
@ViewBag.Message



So now with the above code that i've given you...the following routes work http://localhost:54425/AppDevelopment/Info/MyMvcApp[^]

http://localhost:54425/AppDevelopment/Info/My%20Second%20App[^]

And the URL that you were looking for based on your question

http://localhost:54425/AppDevelopment/Info/Apps[^]

Let me know if you have any questions on the code above i'd be happy to help you understand it


I have created different controller and action method.

then i used redirecttoroute(name) inside action method.

like below:

AppDevelopment Controller:

public ActionResult Info(string Id)
{
return RedirecttoRoute(Id);
}

Thanks friends,

Regards
Lalitha


Where Default Routeconfig.cs

routes.MapRoute(
               name: "Default",
               url: "{controller}/{action}/{id}",
               defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
           );


{id}:Option parameter
No need to fix from here
See this MapRoute setting will effect on page load time.
By default id is optionparamter and where you are fixing with "Apps" value that's why id is coming with URL


这篇关于路由无法在MVC3中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆