gpt4 book ai didi

c# - 如何更改 MVC4 网站的 url?

转载 作者:行者123 更新时间:2023-11-30 18:46:44 25 4
gpt4 key购买 nike

我需要更改地址栏上的 URL

我寻找了 URL Rewrite 但据我所知它适用于这样的请求:

url.com/mypage.aspx?xp=asd&yp=okasd

并将其转换为:

url.com/mypage/asd/okasd

http://www.iis.net/downloads/microsoft/url-rewrite

那不是我的范围。我已经有了 MVC Routesurl.com/mypage 一起工作。问题是,当我键入 URL 时,我被重定向(这是我想要的行为)到 url.com/otherpage/actionX?param=value。问题是我仍然希望地址栏显示 url.com/mypageURL Rewrite 会为此工作吗?我问是因为我不知道它是否有效,因为它是内部重定向 (RedirectToAction) 而不是“常规”访问。如果有人想知道为什么我不能为此制定路线,正如我的问题中所解释的那样,我已经为 url.com/mypage 制定了一条规则,该规则重定向到决定采取什么行动的“路由器”打电话。

我看到了一些问题,但我认为它们没有涵盖我的具体问题:

MVC3 change the url

C# - How to Rewrite a URL in MVC3

更新

这是我的路线:

routes.MapRoute(
"Profile", // Route name
"{urlParam}", // URL with parameters
new { controller = "Profile", action = "Router" } // Parameter defaults
);

Router 操作中,我根据在 urlParam 上完成的一些验证重定向到正确的操作。我需要这种行为,因为每个操作都会返回不同的 View

更新了我的标签,因为我现在使用的是 MVC4

谢谢。

最佳答案

我曾经不得不在 Windows 机器上运行一个 PHP 站点。在它最初运行的 linux 机器上,它有一个重写定义,使站点只在一个 php 文件 (index.php) 中处理所有请求。

我已经使用以下参数安装并配置了 URL Rewrite

Name              : all to index.php
Match URL ------------------------------
Requested URL : Matches the Pattern
Using : Regular Expressions
Pattern : (.*)
Ignore Case : Checked
Conditions -----------------------------
Logical Grouping : Match All
Input : {REQUEST_FILENAME}
Type : Is Not a File
Action ---------------------------------
Action Type : Rewrite
Action Properties:
Rewrite Url : /index.php?$1
Append Query String : Checked
Log Rewritten URL : Checked

这使得对站点的所有请求(css 和 js 文件除外)都由 index.php 处理

所以 url.com/user/1 在服务器端被处理为 url.com/index.php?/user/1因为它在服务器端工作,客户端 url 保持不变。

使用它作为你的基础,你可以构建一个重写(不是重定向)。

关于c# - 如何更改 MVC4 网站的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15787095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com