使用 web.config 中的 httpRedirect 将 asp.net MVC URL 重定向到特定的 controller/action/parameter
Redirecting an asp.net MVC URL to a specific controller/action/parameter using httpRedirect in web.config
我在 Azure 中托管了一个网站,可以使用 URL https://abc.cloudapp.net/controller1/action1/parameter1
访问该网站
我的客户想要以下内容。
当我们尝试访问 url“https://abc.cloudapp.net”时,它应该会自动将我重定向到上面提到的实际 url 网站。
我希望使用 web.config 来实现。我尝试了几种组合(下面列出了其中的几个)。
组合 1:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to Full URL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URL}" pattern="^abc.cloudapp.net$" />
</conditions>
<action type="Redirect" url="https://abc.cloudapp.net/controller1/action1/parameter1" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
组合二:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to Full URL" stopProcessing="true">
<match url="https://abc.cloudapp.net" />
<action type="Redirect" url="https://abc.cloudapp.net/controller1/action1/parameter1" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
None 这些组合有效。
我已经在下面发布了针对此问题的解决方法。
但是,我需要一些快速帮助以获得永久解决方案。
我仍然需要帮助,请有人帮忙。
我找到了解决方法。
当我们使用 url“https://abc.cloudapp.net”时,使用默认路由配置,我们可以将其设置为转到 controller1 中的 action1。
然后在"controller1"的"action1"中如果我们看到"parameter1"为null(必须声明为可空类型),我们需要将parameter1初始化为已知值并重定向到 "controller1"
中的相同 "action1"
public ActionResult Index(int? parameter1)
{
if(string.IsNullOrEmpty(parameter1)
return RedirectToAction("action1", "controller1", new {@parameter1 = "knownValue"});
else
{
//whatever action you wanted to do based on value in parameter1
}
}
然而,整个方法需要硬编码,我们正在为 paramter1 分配一个已知值。如果这个值有变化,我们需要更改代码
因此我不推荐这个。但是现在我已经使用了这个解决方法。我仍然需要一个适当的解决方案来解决这个问题。
我的研发还在继续……………………
我在 Azure 中托管了一个网站,可以使用 URL https://abc.cloudapp.net/controller1/action1/parameter1
访问该网站我的客户想要以下内容。
当我们尝试访问 url“https://abc.cloudapp.net”时,它应该会自动将我重定向到上面提到的实际 url 网站。
我希望使用 web.config 来实现。我尝试了几种组合(下面列出了其中的几个)。
组合 1:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to Full URL" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{URL}" pattern="^abc.cloudapp.net$" />
</conditions>
<action type="Redirect" url="https://abc.cloudapp.net/controller1/action1/parameter1" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
组合二:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to Full URL" stopProcessing="true">
<match url="https://abc.cloudapp.net" />
<action type="Redirect" url="https://abc.cloudapp.net/controller1/action1/parameter1" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
None 这些组合有效。 我已经在下面发布了针对此问题的解决方法。 但是,我需要一些快速帮助以获得永久解决方案。 我仍然需要帮助,请有人帮忙。
我找到了解决方法。
当我们使用 url“https://abc.cloudapp.net”时,使用默认路由配置,我们可以将其设置为转到 controller1 中的 action1。
然后在"controller1"的"action1"中如果我们看到"parameter1"为null(必须声明为可空类型),我们需要将parameter1初始化为已知值并重定向到 "controller1"
中的相同 "action1"public ActionResult Index(int? parameter1)
{
if(string.IsNullOrEmpty(parameter1)
return RedirectToAction("action1", "controller1", new {@parameter1 = "knownValue"});
else
{
//whatever action you wanted to do based on value in parameter1
}
}
然而,整个方法需要硬编码,我们正在为 paramter1 分配一个已知值。如果这个值有变化,我们需要更改代码 因此我不推荐这个。但是现在我已经使用了这个解决方法。我仍然需要一个适当的解决方案来解决这个问题。
我的研发还在继续……………………