ASP.NET - 无法查找视图

时间:2013-11-21 15:51:52

标签: asp.net-mvc

如果有正斜杠作为参数,即使它们是URL编码的,ASP.NET MVC也存在一个长期存在的问题,即没有正确处理URL路由。

例:
在默认安装中,转到此URL(根据需要调整端口)

http://localhost:11541/Token/Create?
callback=http%3a%2f%2flocalhost%3a11491%2ftoken%2fcreatetoken%2fAddPrivateValues

请注意,Controller是“Token”,“Create”是要调用的方法。这是我得到的错误:

 The view 'http://localhost:11491/token/createtoken/AddPrivateValues' or
 its master was not found or no view engine supports the searched
 locations. The following locations were searched:
 ~/Views/Token/http://localhost:11491/token/createtoken/AddPrivateValues.aspx
 ~/Views/Token/http://localhost:11491/token/createtoken/AddPrivateValues.ascx
 ~/Views/Shared/http://localhost:11491/token/createtoken/AddPrivateValues.aspx
 ~/Views/Shared/http://localhost:11491/token/createtoken/AddPrivateValues.ascx

请注意,它正在调用“CreateToken / AddPrivateValues”。这是错的。它应该是调用Token.Create。

这个问题似乎自2009年以来一直被打破(根据先前的S.O.研究),所以我没有屏住呼吸。我只需要修复此问题并将其移至Azure。

我尝试将{* id}路由添加到我的控制器,但这不起作用,因为有很多正斜杠。 only way to fix this is to disable this parsing in the machine.config(web.config不会工作)

问题

如何在不使用RDP的情况下在Windows Azure中设置此属性,以便Web.config上的权限与我尝试执行此操作之前一样安全并锁定?

1 个答案:

答案 0 :(得分:0)

我无法在清理部署MVC 4项目时复制您的问题。你是如何尝试将回调参数传递回视图的(如果有的话)。如果您正在使用

return View(callback);

这就是它失败的原因,因为它将字符串变量解释为视图名称。尝试将其存储在视图包中或将其封装在视图模型中。作为旁注,我知道使用斜杠传递参数似乎适用于所有MVC版本,因为在帐户/登录控制器操作中,您可以毫无问题地发送returnUrl。

以下是我的配置,如果我偏离了您的设置,请告诉我。

路线配置

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    //Changed default route to allow me to only have to create one controller
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Token", action = "Index", id = UrlParameter.Optional }
    );
}

令牌控制器

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Create(string callback)
    {
        @ViewBag.Item = callback;
        return View();
    }

<强> Create.cshtml

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@ViewBag.Item

我的网址:http://localhost:11319/Token/Create?callback=http%3a%2f%2flocalhost%3a11491%2ftoken%2fcreatetoken%2fAddPrivateValues

我的创建页面结果

<h2>Create</h2>

http://localhost:11491/token/createtoken/AddPrivateValues


    <script src="/Scripts/jquery-1.8.2.js"></script>