Reputation: 13367
very simple basic question I have only route:
routes.MapRoute(
"Widget", // Route name
"Widget/Frame/{postUrl}", // URL with parameters
new { controller = "Widget", action = "Index", postUrl = UrlParameter.Optional } // Parameter defaults
);
And when i try to open following url:"http://localhost:50250/Widget/Frame/qwerty"
I have an error:
The view 'qwerty' or its master was not found or no view engine supports the searched locations. The following locations were searched:
Well...why?
Controller code:
public class WidgetController : Controller
{
//
// GET: /Widget/
public ActionResult Index(string postUrl, int? blogEngineType)
{
return View(postUrl);
}
}
Upvotes: 0
Views: 2259
Reputation: 16928
It's because your return statement is return View(postUrl);
and when you pass a string to the View() method it is interpreted as the name of the view to use. So it looks for a view called qwerty
since that's what's in that variable. If you want to hand postUrl as a model to the view of your Index action, you'll have to change your return to be return View("Index", postUrl)
Upvotes: 2
Reputation: 552
Are you sure there is a View called 'qwerty' in the Shared or Widget folder within the Views parent folder?
Otherwise you probably want to use return RedirectToAction(postURL);
Upvotes: 1
Reputation: 31250
You are returning a View with
return View(postUrl);
Since there is no name of the view (in this overload), the method uses the Action name as view name and looks for it. You probably meant to do
return Redirect(postURL);
Upvotes: 3
Reputation: 3326
I would hazard a guess and say it's because it's actually trying to use the action name of Index(), since that's the default action that you've specified. You're not passing an {action} parameter through the url, so where else will it get the action from?
Can you change your url pattern to Widget/{action}/{postUrl}
and see if it works then?
Either that, or set the default value of action
to Frame
instead. Basically, it has no way of knowing that you're looking for the Frame
action, so it fails.
Edit: I see what you're doing now - the action's name is actually Index, right? In that case, I'm not sure, we need to see your controller code. I'll leave the above answer in case it's useful.
Edit 2: You're passing the value "qwerty" as the view name - do you have a view named "qwerty" in the views folder?
If you intend for it to be the model, and for the view name to be "Index", you should call return View((object)postUrl);
instead, so that it doesn't get confused.
Upvotes: 2