MVC3查看路由和局部视图没找到 [英] MVC3 View Routing and partial view not found

查看:144
本文介绍了MVC3查看路由和局部视图没找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我的问题,但不知道如何解决它,所以我在这里希望天才能有所帮助。我有我想要抓住一块信息从URL然后重定向到该网站的控制器。我的控制器执行它的东西,并返回看法,但认为有部分在它和视图引擎无法找到它?

I know what my problem is but not sure how to fix it so I was hoping the geniuses here could help. I have a controller that I want to grab a piece of info from the url then redirect to the site. My controller does it's thing and returns the view but the view has a partial in it and the view engine can't find it?

因此​​,这里是设置。

So here is the setup.

控制器被映射路线为:

//marketing/tracking
routes.MapRoute("CampaignTracking",
                "{save}/{campaignid}",
        new { controller = "CampaignTracking", action = "Index" });

这个伟大的工程。所以下次我的控制器:

This works great. So next my controller:

public ActionResult Index(Int32 campaignID)
{
        var model = new ...snip...

        return View("../Customer/Login", model);
}

现在我本来得到了错误找不到看法,因为它一直在寻找一个CampaignTracking文件夹,我有:

Now I originally got the error couldn't find view as it was looking in a CampaignTracking folder and I had :

return View("Customer/Login", model);

所以我改变了它上面所看到的../才能到正确的位置。现在它找到Login.cshtml但在login.cshtml它呈现一个局部,并再次它正在寻找来发现Campaigntracking部分

So I changed it as seen above with the ../ to get to the correct location. Now it finds the Login.cshtml but in login.cshtml it renders a partial and again it is looking to find the partial in the Campaigntracking.

我可以取消CampaignTracking控制器和作用的结果搬进CustomerController,但会preFER保持它在它自己的控制器,因为这很可能会增长,并在客户没有真正的组。

I could abolish the CampaignTracking controller and move the action result into the CustomerController but would prefer to keep it in its own controller as this will likely grow and doesn't really group under customer.

按NICU Janga

As per nicu Janga

我在我的控制器更改:

return View(@"~/Views/Customer/Login.cshtml", model);

在我login.cshtml谐音:

and in my login.cshtml the partials:

@Html.Partial(@"~/Views/Customer/...etc.cshtml")

但我想知道这是为什么。我能理解视图引擎如何被弄得出来的控制器,但一旦与返回查看修正(@〜/查看/客户/ Login.cshtml模型);不应该正确的路径传播,以及对login.cshtml ???

but I am wondering why. I can understand how the view engine gets confused coming out of the controller but once corrected with return View(@"~/Views/Customer/Login.cshtml", model); shouldn't the proper path propagate as well to the login.cshtml???

显然不是,但什么都不明白我??

Obviously not but what don't I understand??

推荐答案

让我看看,如果我能在一些细节填写。首先亮相,让我们说,我们刚刚增加了以下控制器空项目:

Let me see if I can fill in some details for you. First up, let's say we've just added the following controller to an empty project:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

现在,让我们假设,我们并没有刻意去添加 Index.cshtml 视图,所以当我们运行它,它会告诉我们的观点丢失。看看伴随该异常的消息:

Now, let's also assume that we didn't bother to add the Index.cshtml view, so when we run this, it will tell us the view is missing. Take a look at the message that accompanies that exception:

The view 'Index' or its master was not found or no view engine supports
the searched locations. The following locations were searched:
~/Views/home/Index.aspx
~/Views/home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/home/Index.cshtml
~/Views/home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

这是当MVC是试图找到一种观点认为,被搜索的确切位置。在大多数情况下,如果你使用C#与MVC中,只有两个,你需要关心的是:

These are the exact locations that are searched when MVC is trying to locate a view. For the most part, if you're using C# with MVC, the only two you need to care about are:

~/Views/home/Index.cshtml
~/Views/Shared/Index.cshtml

这意味着,如果你指定视图的名字时,你不提供一个路径,它总是会检查这两个位置。因此,如果我们这样做是从我们的的HomeController

That means if you don't provide a path when specifying the name of your view, it will always check both of these locations. So if we did this from our HomeController:

return View("Test");

MVC将寻找这个观点在〜/查看/主页/ Test.cshtml 然后〜/查看/共享/ Test.cshtml 。关于这个伟大的事情是它的相一致的。它总是会在相同的方式的意见。

MVC will look for this view at ~/Views/Home/Test.cshtml and then ~/Views/Shared/Test.cshtml. The great thing about this is it's consistent. It always looks for the views in the same way.

现在,让我们使用的更复杂的例子(假设我们在我们的的HomeController

Now, let's use a more complicated example (again, assuming we're in our HomeController):

return View("../SomeDirectory/Test.cshtml");

这是要在以下几个方面来看待的:

This is going to be looked for in the following ways:

~/Views/Home/../SomeDirectory/Index.cshtml
~/Views/Shared/../SomeDirectory/Index.cshtml

考虑看看〜/查看/首页/../ SomeDirectory / Index.cshtml 第一,这将实际上意味着它寻找这里的观点:〜/查看/ SomeDirectory / Index.cshtml 。显然,这是不是您的看法是,这同样适用于其中,〜/查看/共享/../ SomeDirectory / Index.cshtml 结束。

Taking a look at ~/Views/Home/../SomeDirectory/Index.cshtml first , this would actually mean it's searching for the view here: ~/Views/SomeDirectory/Index.cshtml. Obviously, this isn't where your view is, and the same applies to where ~/Views/Shared/../SomeDirectory/Index.cshtml ends up.

这里要记住的重要一点是意味着你的网站相对根。它总是会从您的网站,这意味着你总是可以知道到底发生了什么的根本工作。通过这样做,

The important thing to remember here is ~ means the relative root of your site. It will always work from the root of your site, which means you can always be sure exactly what's going on. By doing this

return View("~/Views/Home/Index.cshtml");

你告诉MVC的究竟在何处找到你的看法的,而不是让公约的MVC的规则,决定你。这意味着无论何时您指定的位置MVC找到你的看法,它会搜索该位置。因此,返回查看(〜/查看/主页/ Index.cshtml)搜索视图在〜 /Views/Home/Index.cshtml 。如果无法找到视图那里,你会像以前一样得到同样的例外,但它会给你一个稍微不同的消息:

you are telling MVC exactly where to find your view, rather than letting MVC's rules of convention decide for you. That means whenever you specify a location for MVC to find your view, it will only search that location. So return View("~/Views/Home/Index.cshtml") will only search for the view at ~/Views/Home/Index.cshtml. If it cannot find the view there, you'll get the same exception as before, but it will give you a slightly different message:

The view '~/Views/Home/Index.cshtml' or its master was not found or no view
engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.cshtml

注意它是如何不搜索在其他地点的看法。

Notice how it hasn't searched for the view in the other locations.

这篇关于MVC3查看路由和局部视图没找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆