使用web.config中的httpRedirect将asp.net MVC URL重定向到特定的控制器/操作/参数

时间:2016-05-17 14:31:26

标签: c# asp.net-mvc asp.net-mvc-4 azure http-redirect

我在Azure中托管了一个网站,该网站将通过网址https://abc.cloudapp.net/controller1/action1/parameter1

进行访问

我的客户需要以下内容。

当我们尝试访问网址" https://abc.cloudapp.net"时,它会自动将我重定向到上面提到的网站的实际网址。

我希望使用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>

组合2:

<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>

这些组合都没有奏效。 我已经针对此问题发布了下面的解决方法。 但是,我需要一些快速帮助才能获得永久解决方案。 我仍然需要帮助,有人可以帮忙。

1 个答案:

答案 0 :(得分:0)

我找到了解决方法。

当我们使用网址“https://abc.cloudapp.net”时,使用默认路由配置我们可以将其设置为在controller1中转到action1。

然后在“controller1”的“action1”中,如果我们看到“parameter1”为空(必须被称为可空类型),我们需要将parameter1初始化为已知值并重定向到相同的“action1”在“controller1”中

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分配一个已知值。如果此值发生变化,我们需要更改代码 因此我不推荐这个。但是现在我已经使用了这种解决方法。我仍然需要解决这个问题。

MY R&amp; D继续...... ...... ...... ......

相关问题