我想通过mvc路由,把一个请求的图片地址返回别个网站的,能否实现呢

比如别人请求的   ,我在mvc中做一个路由,想把 http://www.abc.com/upload/123.jpg 在路由上变成 http://img.abc.com/upload/123.jpg

routes.MapRoute(
"getImg", // 路由名称
"upload/{strPath}.jpg", // 带有参数的 URL
new { controller = "Home", action = "getRealPath",strPath=UrlParameter.Optional } // 参数默认值
);

这个路由还没测试是否可行,但已经看到不能捕捉到 www.abc.com ,也就是会变成 

在 MVC 中,路由模式是从 URL 中的控制器、动作和参数推断出路由信息的。因此,您的路由配置需要匹配 URL 中的控制器和动作,并可以指定参数。


如果要更改 URL 的域名,可以在视图中使用 UrlHelper 类或 Razor 语法中的 @Url.Action() @Html.ActionLink() 方法来生成 URL。


例如,可以使用以下代码在视图中生成 URL:

@Html.ActionLink("图像链接", "getRealPath", "Home", new { strPath = "123" }, null)

这将生成以下 HTML:

<a href="/Home/getRealPath/123">图像链接</a>

要将域名更改为 img.abc.com,可以使用以下代码:

@Html.ActionLink("图像链接", "getRealPath", "Home", new { strPath = "123" }, new { @href = "http://img.abc.com/Home/getRealPath/123" })

这将生成以下 HTML:

<a href="http://img.abc.com/Home/getRealPath/123">图像链接</a>

此外,还可以使用 UrlHelper 类中的 Action() 方法在代码中生成 URL,例如:

string url = Url.Action("getRealPath", "Home", new { strPath = "123" }, "http");

这将生成以下 URL:

http://localhost/Home/getRealPath/123

您可以使用此 URL 作为图像的 src 属性,例如:

<img src="@Url.Action("getRealPath", "Home", new { strPath = "123" }, "http")" />

最后,还可以使用 Web.config 文件中的 URL重写配置来更改 URL 域名。


URL 重写是一种使用规则将原始 URL 转换为新 URL 的技术。可以使用 URL 重写来更改 URL 的域名。


要使用 URL 重写,需要安装并配置 URL 重写模块。有许多可用的 URL 重写模块,包括 Microsoft URL Rewrite 模块和 Apache mod_rewrite 模块。


例如,要使用 Microsoft URL Rewrite 模块将所有请求转发到 img.abc.com 域,可以在 Web.config 文件中添加以下规则:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Redirect to img.abc.com" stopProcessing="true">
        <match url=".*" />
        <conditions>
          <add input="{HTTP_HOST}" pattern="^www\.abc\.com$" />
        </conditions>
        <action type="Redirect" url="http://img.abc.com/{R:0}" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

此规则将所有请求从 http://www.abc.com/ 域转发到 img.abc.com 域。


请注意,使用 URL 重写可能会对性能造成影响,因此应谨慎使用。