asp.net mvc重写url地址
程序员文章站
2023-12-29 09:49:10
...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using WebGrease.Css.Ast;
namespace WebApplicationUrlWrite
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//加载重写
routes.Add(new MyRoute());
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
public class MyRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
return null;
//var virtualPath = httpContext.Request.AppRelativeCurrentExecutionFilePath + httpContext.Request.PathInfo;//获取相对路径
//var data = new RouteData(this, new MvcRouteHandler());//声明一个RouteData,添加相应的路由值
//data.Values.Add("controller", "Category");
//data.Values.Add("action", "ShowCategory");
//data.Values.Add("id", 1);
//return null;//返回这个路由值将调用CategoryController.ShowCategory(category.CategoeyID)方法。匹配终止
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
string token = requestContext.HttpContext.Request.QueryString["uck"];
string controller = values["controller"].ToString();
string action = values["action"].ToString();
//处理Html.Action传递多参数
//<a href="@Url.Action("About","home",new{articleId= "asldjfsjd", phone = "13588877234" , address = "武侯大道" ,pageIndex=1,pageSize=10})">关于2</a>
StringBuilder sb = new StringBuilder();
foreach (var item in values.Keys)
{
if (item.Equals("controller") || item.Equals("action"))
{
continue;
}
var _kvalue = values[item];
sb.Append("&" + item + "=" + _kvalue);
}
string url = controller + "/" + action + "?uck=" + token + sb.ToString();
return new VirtualPathData(this, url);
}
}
}
查看生成url:
推荐阅读
-
asp.net mvc重写url地址
-
[ASP.NET MVC 小牛之路]07 - URL Routing
-
ASP.NET MVC5实现文件上传与地址变化处理(5)
-
ASP.NET MVC中URL地址传参的两种写法
-
asp.net下用url重写URLReWriter实现任意二级域名的方法第1/2页
-
在ASP.NET中重写URL的代码
-
ASP.NET MVC实现功能:通过URL路由实现对多语言的支持
-
ASP.NET MVC实现功能:通过URL路由实现对多语言的支持
-
ASP.NET实现MVC中获取当前URL、controller及action的方法
-
asp.net 2.0 中的URL重写以及urlMappings问题