dsb
dsb

Reputation: 2527

Action does not find View or its master

I have the following action:

public class ArticlesController : Controller
{

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

which returns the view and everything seems to work fine.

Now I try to add ActionName:

    [ActionName("bla-bla-article-1")]
    public ActionResult Article01()
    {
        return View();
    }

Now calling: /Article/Article01 returns

'The resource cannot be found.'

Now I try this:

[ActionName("bla-bla-article-1")]
public ActionResult Article01()
{
    return View("~/Content/Views/Articles/Article01.cshtml");
}

And here I get:

The view '~/Content/Views/Articles/Article01.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Content/Views/Articles/Articles01.cshtml

Upvotes: 0

Views: 156

Answers (1)

haim770
haim770

Reputation: 49133

You have to realize that by default, the method name is also the action name. But, once you're overriding that convention by using the [ActionName] attribute, the Url through which you access the action is subject to change as well.

In your case that would probably be:

/Articles/bla-bla-article-1

And, when you're using return View() without specifying the view name, it's being determined automatically from the current route parameters, and after your attribute is applied, the value would be bla-bla-article-1.

That's why you have to specify it explicitly:

return View("Article01");

See MSDN

Upvotes: 1

Related Questions