Routing in MVC 4 - asp.net-mvc-4

Mine is simple scenario but I am confused on how routing works.
I have three controllers SearchHome, Home and Account.
Here is route config.
routes.MapRoute("default", "{controller}/{action}/{name}",
new { controller = "SearchHome", action = "Home", name = UrlParameter.Optional }, new[] { "AppName" }
);
I do not want to show my controller name in URL. So, I take away controller in my above code and make it as below.
routes.MapRoute("default", "{action}/{name}",
new { controller = "SearchHome", action = "Home", name = UrlParameter.Optional }, new[] { "AppName" }
);
Controller Name is not showing for SearchHome controller views but Home and Account Views are showing up at all.
What am I doing wrong ? Can I write a seperate route for each controller ?
Here is the same issue but The answer is not working for me.
ASP.NET MVC - Removing controller name from URL

Related

How to handle Durandal MVC routing?

I have introduced mvc area concept with the durandal
My hierarchy would be
Area.Web
Areas
Blog
Controller
SocialController
Social
View
Task
Controller
View
Scripts
App
Blog
ViewModels
I have route to the area based on my url. For example, localhost/blog
My route woule be:
routes.MapRoute(
name: "blog",
url: "blog/{action}/{id}",
defaults: new { controller = "blog", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Area.Web.Blog.Controllers" }
);
routes.MapRoute(
name: "Task",
url: "Task/{action}/{id}",
defaults: new { controller = "task", action = "Index", id = UrlParameter.Optional},
namespaces: new string[] { "Area.Web.Task.Controllers" }
);
When I try to navigate to localhost/blog, it call the correct controller and render the correctly. when the durandal route happen blog is excluded.So that my route url not able to fetch the controller.
app.setRoot('viewmodels/social/home', 'entrance');
The following route throws 404 exception, since it ignored the blog in the route url (localhost/social/home)
Please let me know, how to resolve this issue. Is it possible to include area name in all the route and app.set.
Firstly IMHO You should reconsider Your project architecture. For example, if you need to optimize this app with Weyland, both SPAs will end up in the same file what does not gives You required separation.
However, instead of having them both under the app folder, you can make separate folders, and give each SPA it's own main.js file.
In such case You would have following project structure:
Area.Web
AppBlog
Services
Views
ViewModels
main.js
AppTask
Services
Views
ViewModels
main.js
Areas
Blog
Controller
View
Task
Controller
View
Scripts
durandal
etc.
Content
etc.
Your View in Blog Area would use follwoing JS import:
<script src="~/Scripts/require.js" data-main="#Url.Content("~/AppBlog/main")"></script>
while Task Area would use
<script src="~/Scripts/require.js" data-main="#Url.Content("~/AppBlog/main")"></script>
In such setup Your routes would look similar to exactly the same as in Your code
EDIT
routes.MapRoute(
name: "blog",
url: "blog/{action}/{id}",
defaults: new { controller = "home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Area.Web.Blog.Controllers" }
);
routes.MapRoute(
name: "Task",
url: "Task/{action}/{id}",
defaults: new { controller = "home", action = "Index", id = UrlParameter.Optional},
namespaces: new string[] { "Area.Web.Task.Controllers" }
);

Custom route MVC4 with constraints

I have an MVC 4 application that i want to create custom route to respective controllers.
The goal is simply to not show the action name in the url, rather only show controller name and parameters.
routes.MapRoute(
"Account", // Route name
"Account/{param}", // URL with parameters
new { controller = "Account", action = "Account", param = UrlParameter.Optional }, // Parameter defaults
new { controller = #"^(Account)$" } //Parameter constraints
);
routes.MapRoute(
"Login", // Route name
"Login/{returnurl}", // URL with parameters
new { controller = "Login", action = "Login", returnurl = UrlParameter.Optional }, // Parameter defaults
new { controller = #"^(Login)$" } //Parameter constraints
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
At the moment i'm using this route above which work fine until i call another action that is not specified in the default parameter/url of each root.
E.g the route to Login/Login or Login/Login?somevalue works as expected to simply not show the action in the url Logn or Login/somevalue. But as soon another action in the same controller is requested the route resolves to Login/Login/Logout for instance. Why is this occurring when i have set the route to match all requests to Login controller?
All actions in the controller have the returnurl parameter
An explanation or clarification on this matter would be greatly appreciated if some could take time and view my routes.
Any help or thoughts would be nice.
Thanks in advance.

Routing in ASP.NET MVC 4

I have an ASP.NET MVC 4 app. I'm trying to setup my routes in the RouteConfig.cs file. Essentially, I have two URLs I'm currently interested in. Those URLs are:
/App
/App/Auth
My file structure looks like the following:
/Views
App
Index.cshtml
Auth
Index.cshtml
Docs
Index.cshtml
Index.cshtml
In my RouteConfig.cs file, I have the following:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Index",
"Index",
new { controller = "Views", action = "Index" }
);
routes.MapRoute(
"Dashboard", // Route name
"{controller}/auth/{action}/{id}", // URL with parameters
new { controller = "App", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Oddly, when I visit /App/Auth it takes me to /App. For the life of me, I can't figure out why. Does anyone know how to do this? Thanks!
The app is doing exactly what you asked. Your first route goes to controller "App" and action "Index". Move the folder Auth to same level as App and then change your route to:
routes.MapRoute(
"Dashboard", // Route name
"app/auth/{action}/{id}", // URL with parameters
new { controller = "Auth", action = "Index", id = UrlParameter.Optional }
);
An even better solution, forget the Auth controller and just create a view auth.cshtml in the App folder and an action "Auth" in the App controller, and then you can delete the route altogether and /app/auth will get handled by the default route.
Change the route to this:
routes.MapRoute(
"Dashboard", // Route name
"App/auth/{action}/{id}", // URL with parameters
new { controller = "App", action = "Index", id = UrlParameter.Optional }
);

Routing Facebook AppRequests in ASP.Net MVC 4

My mobile web site allows users to send an AppRequest to their Facebook friends. This is working.
When the friend accepts the AppRequest, Facebook sends the friend to my web site. This too is working.
My web site is an ASP.Net MVC 4 application. I am trying to get my routes to recognize the incoming AppRequest acceptance but I can't figure out how to do it.
Facebook is sending the friends to my site using this URL:
http://www.example.com/?ref=notif&code=abcdefg&fb_source=notification
This keeps getting routed to Home/Index despite my attempts to map the route to a custom controller and action. Here is what I have done so far that has failed to work:
Registered Routes:
routes.MapRoute(
name: "FacebookAppRequest",
url: "{ref}/{code}/{fb_source}", //This should match the URL above
defaults: new { controller = "Facebook", action ="FBAppRequestHandler"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Controller:
public class FacebookController : Controller
{
public FacebookController() {}
public ActionResult FBAppRequestHandler(
[Bind(Prefix = "ref")] string fbReferal,
[Bind(Prefix = "code")] string fbCode,
[Bind(Prefix = "fb_source")] string fbSource)
{
//Do some stuff with fbReferal, fbCode and fbSource
return View();
}
The ref the code and the fb_source are passed as query string parameters. They are not part of the route. So you cannot possibly expect that {ref}/{code}/{fb_source} would match your custom route. That would have been the case if the request looked like that:
http://www.example.com/notif/abcdefg/notification
Since the actual route looks like this (forget about query string parameters - they are not used for routing):
http://www.example.com/
all that you have here basically is the following url /. So the best you could hope here is to modify your default route so that it routes to the desired controller:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Facebook", action = "FBAppRequestHandler", id = UrlParameter.Optional }
);
Now get rid of the first route - it's not necessary.

ASP.NET MVC URL Routing with parameters in the middle

I have a controller called Quote, with an Index method that requires a requestId parameter. The URL appears as such
/Quote/{requestId}.
Additionally, I have a method called ApplicantInfo which is specific to the quote and routes as such
/Quote/{requestId}/ApplicantInfo.
But when I use the Url.Action helper like so
#Url.Action("Index","Quote",new { requestId = {requestId}})
It gives me a url of
/Quote/ApplicantInfo?requestId={requestId}
which does not route correctly.
Obviously, I can manually create the URL string, but before I throw in the towel I wanted to know if I was missing something, for instance an override to the Url.Action method that will output the correct Url.
TIA
ROUTES
routes.MapRoute(
"QuoteInfo",
"Quote/{requestid}",
new { controller = "Quote", action="Index" });
routes.MapRoute(
"QuoteApplicant",
"Quote/{requestid}/ApplicantInfo",
new { controller = "Quote", action = "ApplicantInfo" });
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action="Index", id = UrlParameter.Optional });
I was able to get do something similar to this like this
Route
Define a route in Global.asax.cs or whereeve you override your routes
routes.MapRoute(
"Organization_default",
"{controller}/{requestId}/{action}",
new {
controller = "home",
action = "index",
requestId = "default",
id = UrlParameter.Optional
}, null);
Controller
public ActionResult Question(string requestId)
{
ViewData["value"] = requestId;
return View();
}
View
#{
ViewBag.Title = "Index";
var value = ViewData["value"];
}
<h2>Stackoverflow #value</h2>
#Html.ActionLink("Click Here",
"question", "StackOverflow", new { requestId ="Link" }, new{ #id="link"})
Screenshot
screenshot of how the links appear with this route, I defined the
Catch
You CANNOT have another route as {controller}/{action}/{key} defined before your custom this route. If you do the other route override your custom route.
If you need both the routes to co-exist then you would have to define a separate Area and define your custom route overriding RegisterArea
public override void RegisterArea(AreaRegistrationContext context)
{
//..custom route definition
}