problem with routing asp.net - asp.net-mvc

I am fairly new to asp.net and I have a starting URL of http://localhost:61431/WebSuds/Suds/Welcome and routing code
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{page}", // URL with parameters
new { controller = "Suds", action = "Welcome", page = 1 } // Parameter defaults
);
routes.MapRoute(
"Single",
"{controller}/{action}",
new { controller = "Suds", action = "Welcome" }
);
I am receiving the following error: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Can anyone please help me figure out how to route the beginning url to the controller.

Because the first part of your URL is WebSuds, the framework is trying to map the request to WebSudsController instead of SudsController. One thing you can try is to change the url parameter of your route to "WebSuds/{controller}/{action}".

Route tables are searched on a first come first served basis. Once an appropriate match is found any following routes will be ignored.
You need to add "WebSuds/{controller}/{action}" and put it above the default route. The most specific routes should always go above the more generic ones.

With the following Routes and Url you have given make sure you have these Methods in your Controller
public class SudsController
{
public ActionResultWelcome(int? page)
{
return View();
}
}
Since you have created two routes of the same Action, one with a parameter page and another without. So I made the parameter int nullable. If you don't make your parameter in Welcome nullable it will return an error since Welcome is accepting an int and it will always look for a Url looks like this, /WebSuds/Suds/Welcome/1. You can also make your parameter as string to make your parameter nullable.
Then you should have this in your View Folder
Views
Suds
Welcome.aspx
If does doesn't exist, it will return a 404 error since you don't have a corresponding page in your Welcome ActionResult.
If all of this including what BritishDeveloper said. This should help you solve your problem.

Related

MVC 4 routing to a controller

I am new to MVC and I am trying to mess around by creating a practice site which will be a gallery site for viewing and uploading images. The problem I encountered is that I cannot get the routing to work correctly.
Here is a link to my routing code and solution tree:
https://imgur.com/a/Oc1Tt?
Did I set the views and controller up incorrectly?
The error I get is: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
Thanks for any input
Routing works by converting an incoming request to route values or by using route values to generate a URL. The route values are either set as parameters in the URL itself, as default values, or both (in which the defaults make the URL parameters optional).
You have not set any route values in your route. Since you don't have route parameters in the URL, you need to set defaults (controller and action are required by MVC).
routes.MapRoute(
name: "Gallery",
url: "Gallery/Index",
defaults: new { controller = "Gallery", action = "Index" }
);
That said, your Default route already covers this URL. You only need to add custom routes if you desire behavior that the Default route doesn't cover.
Also, if you change the view names so they don't match the name of the action method, you have to specify the name explicitly from the action method.
public ActionResult Index()
{
return View("~/Views/Gallery/GalleryView.cshtml");
}
By default MVC uses conventions. It is much simpler just to name the view Index.cshtml instead of GalleryView.cshtml so you can just return View from the action method.
public ActionResult Index()
{
return View();
}

routing with just one of two parameters of controller

I have an actionresult with two parameter:
public ActionResult Index(int a,string b)
{
//some code
return View(b);
}
it creates this url automatically:
mysite.com/a=1&b=http://site.com/b=1
I just need to show first parameter "a" in my url:
mysite.com/a=1
I use the default route of MVC that creates in global.ascx:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
what should i do?
Thanks...
If you are seeing the "b" parameter bleed through from the current request, you can set the "b" parameter explicitly to empty string to avoid this behavior:
#Html.ActionLink("Home", "Index", "Home", new { a = 1, b = "" }, null)
I reported this "feature" as a bug, but the developers at Microsoft seem to think this behavior is supposed to make your URLs easier to configure, and they don't intend to fix it.
What you're seeing here is a feature of routing where "ambient" values (i.e. values that were detected in the incoming request's URL) are used to simplify (sometimes...) the generation of routes to other pages.
You can check our my answer (under the name "Eilon") in this StackOverflow post, where I explain the behavior in a bit more detail:
How Can I Stop ASP.Net MVC Html.ActionLink From Using Existing Route Values?
Ultimately if you want the most control over what gets generated for a URL there are a few options to consider:
Use named routes to ensure that only the route you want will get used to generate the URL (this is often a good practice, though it won't help in this particular scenario)
Specify all route parameters explicitly - even the values that you want to be empty. That is one way to solve this particular problem.
Instead of using Routing to generate the URLs, you can use Razor's ~/ syntax or call Url.Content("~/someurl") to ensure that no extra (or unexpected) processing will happen to the URL you're trying to generate.
Thanks,
Eilon

How can I achieve clean URL routing with custom user ID?

My ASP.NET MVC site allows users to register and give themselves user names, which will be unique and allow others to browse their pages with a clean URL that includes their name, like Twitter, Facebook, LinkedIn etc. do.
For example:
mysite.com/michael.guthrie
mysite.com/john
mysite.com/john/images
mysite.com/john/blog
etc.
The problem is that the first URL segment might be used for other "regular" controllers/actions, like:
mysite.com/about
mysite.com/register
So basically I seek for a routing scheme that says something like: If the first URL segment is a known controller, treat it as a controller (and parse the relevant action and parameters as usual), but if not - treat it as a user name, and pass it to a dedicated controller+action which will parse it and continue accordingly.
I don't want a solution that will enforce me to add routes for every specific controller that I have, such that after the routing module will go over all of them and won't find a match, it will get to the last one which defines a route for this special user name segment. The reason is primarily maintenance (I must remember to add a route every time I code a new controller, for example.)
I assume I can implement my own MvcRouteHandler / IRouteHandler but I feel there must be simpler solution that won't have me tweak MVC's out-of-the-box routing mechanism.
Note: I've read How to achieve nice litle USER page url like facebook or twitter? and it doesn't answer my question, it's just says that there is a URL rewriting module.
Do you know any good, elegant, clean way to achieve that?
You should have your first route be your Usesr route, with a route constraint along the lines of what I described in this answer: MVC routing question.
If your route is in the form {username}/{controller}/{id}, this route should cover all contingencies.
in the global.asax file you can map your routes
in the registerRoutes() method you can do something like this:
routes.MapRoute(
"ToonStudenten", // Route name
"{controller}/{action}/{userID}, // URL with parameters
new { controller = "Docent", action = "ToonStudenten", userID = UrlParameter.Optional} // Parameter defaults
);
I believe you can change the way your views look with this mapRouting, not entirely sure how though.. will try and search it up
You may want to take a look at this post:
MVC 3 keeping short url
You don't need to set a route for each URL. With a little help from route constraints you can do something like this:
routes.MapRoute(
"Home", // Route name
"{action}", // URL with parameters
new { controller = "Home", action = "Index" }, // Parameter defaults
new { action = "TaskA|TaskB|TaskC|etc" } //Route constraints
);
routes.MapRoute(
"Account", // Route name
"{action}", // URL with parameters
new { controller = "Account", action = "Logon" }, // Parameter defaults
new { action = "Logon|Logoff|Profile|FAQs|etc" } //Route constraints
);

ASP.NET MVC routing issues

I'm having some issues trying to setup my Routing in MVC. I think I understand how it works, but I just can't seem to set the proper paths.
Basically I want to do something similar to how StackOverflow works so:
http://localhost/faq
I want this to grab the HomeController, hit the faq action and return the faq view. I can't seem to figure out how to do this.
Also, I tried adding a new route for something like this:
http://localhost/Boxes/25
So, Boxes is the controller, 25 is obviously the id(parameter). Similar to how stackoverflow has: https://stackoverflow.com/questions/[question number]/[question title]
So I tried doing this:
routes.MapRoute(
"Boxes",
"Boxes/{boxnumber}",
new {
action="Details", cubenumber = ""
}
);
with no success.
I've also downloading the Route Tester app, but that doesn't seem to be helping at this point. Most likely I need to really read up on how routing works, but was just wondering if someone could point me in the right direction right now instead of me having to spin my wheels.
Thanks a lot guys!
Try the following:
routes.MapRoute(
null, // optional route name
"faq",
new { controller="Home", action="Faq" } );
routes.MapRoute(
null, // optional route name
"Boxes/{boxnumber}",
new { controller="Boxes", action="Details", boxnumber = ""} );
// Original route, if needed, should come AFTER more specialized routes.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } ); // Parameter defaults
Some notes that may help you understand this better:
the controller and action parameters must be specified, either explicitly in the incoming URL or via defaults that you specify (if missing in incoming URL)
the order of adding routes is significant because the first match will be used for each incoming URL. In the above example, if the original route is added first, the other ones will never be matched (because the original route specifies defaults for all parameterized parts of the URL)
route name is optional, only needed if you are using route names to generate outbound URLs
When you define a route, it has to at minimum contain two pieces of information: a controller and an action. Those values can either come through as a parameter (i.e. a "{parameter}" portion in the URL pattern), or as a default value.
The route example that you pasted above includes an action but it doesn't contain a controller, so it isn't capable of satisfying a request. Since your controller name is "BoxesController", you could simply add "controller='Boxes'" to the default values of that route and you'd be good.
To achieve the faq route, you could simply define a route whose URL was "faq" and had the default values: controller="Home", action="Faq".

How do you change the controller text in an ASP.NET MVC URL?

I was recently asked to modify a small asp.net mvc application such that the controler name in the urls contained dashes. For example, where I created a controller named ContactUs with a View named Index and Sent the urls would be http://example.com/ContactUs and http://example.com/ContactUs/Sent. The person who asked me to make the change wants the urls to be http://example/contact-us and http://example.com/contact-us/sent.
I don't believe that I can change the name of the controller because a '-' would be an illegal character in a class name.
I was looking for an attribute that I could apply to the controller class that would let me specify the string the controller would use int the url, but I haven't found one yet.
How can I accomplish this?
Simply change the URL used in the route itself to point to the existing controller. In your Global.asax:
routes.MapRoute(
"Contact Us",
"contact-us/{action}/",
new { controller = "ContactUs", action = "Default" }
);
I don't believe you can change the display name of a controller. In the beta, the controller was created using route data "controller" with a "Controller" suffix. This may have changed in RC/RTM, but I'm not sure.
If you create a custom route of "contact-us/{action}" and specify a default value: new { controller = "ContactUs" } you should get the result you are after.
You need to configure routing. In your Global.asax, do the following:
public static void RegisterRoutes(RouteCollection routes)
{
...
routes.MapRoute(
"route-name", "contact-us/{action}", // specify a propriate route name...
new { controller = "ContactUs", action = "Index" }
);
...
As noted by Richard Szalay, the sent action does not need to be specified. If the url misses the .../sent part, it will default to the Index action.
Note that the order of the routes matter when you add routes to the RouteCollection. The first matched route will be selected, and the rest will be ignored.
One of the ASP.NET MVC developers covers what Iconic is talking about. This was something I was looking at today in fact over at haacked. It's worth checking out for custom routes in your MVC architecture.
EDIT: Ah I see, you could use custom routes but that's probably not the best solution in this case. Unless there's a way of dealing with the {controller} before mapping it? If that were possible then you could replace all "-" characters.

Resources