MVC3 action method parameter with dot character in the end - asp.net-mvc-3

I've got an interesting issue in my application.
My action method receives string parameter. And it works fine for all cases except the case when the last character in this string is a dot character. IIS shows HTTP 404 error due to can't to find the route for this case (I guess so).
My method receives the names of music bands.
For example.
http://mywebsite/artist/The-Strokes works fine
http://mywebsite/artist/R.I.O. fails as I descripted above.
My route for this controller described as:
routes.MapRoute("", "artist/{artist}", new { controller = "Artists", action = "Name", artist = (string)null });
What the reason of this and how can I fix it?
Thanks a lot.

You can think of use the {*catchall} in your route rules and then handle the param in your action
"artist/{*artist}"
or you can try to follow this article as an another (maybe better approach)

Related

Laravel 5.7 Passing a value to a route in a controller

My controller posts a form to create a new page. After posting the form I need to redirect the user to the new page that will have the contents for that page that were entered in the previous form. If I simply do return view('mynewpageview', compact('mycontent')); where my mycontent is the object used to execute the $mycontent->save(); command, I carry the risk for someone refreshing the url thus posting the same content twice by creating a new page.
Instead I would like to redirect the user to the actual page url.
My route is
Route::get('/newpage/{id}', 'PageController#pagebyid'); and if I use return redirect()->route('/newpage/$pageid'); where $pageid = $mycontent->id; I get Route not defined error.
What would be the solution either to stop someone from resubmitting the content or a correct syntax for passing the parameter?
The correct answer that works for me is -
Give your route a name in the routes file
Then pass the parameters with an array as shown below in the controller.
return redirect()->route('newpageid', ['id' => $pageid]);
With basic (unnamed) routes, the correct syntax was return redirect('/newpage/'.$pageid);
You have already found out you can alternatively use named routes.
Last but not least, thanks for having considered the "double submit" issue! You have actually implemented the PRG pattern :)

Pagination Conundrum

I used Codeigniter to build my site, and everything is going peachy, except when it comes to dealing with pagination. Because database queries are driven by data passed from my URL, this is screwing everything up. I'm SURE I'm overlooking something obvious, but this is my issue:
/events/town/venue/event-name
All fine and works as expected; the method takes the parameters from the URL to get the data and deliver it. But my question is - what do I do when I want to paginate my Events page? I.e.
/events/2
/events/3
...etc
As far as my controller, and my routes are concerned, the page number here is considered a town, which it isn't. How do I get around this?
Many thanks
"Test" your town or "page number" uri segment, if it is integer then
you sure know that you are looking for pagination thing, otherwise
"normal" behavior of your controller/method pattern.
using PHPs is_numeric.
A little pseudo code:
if (is_numeric($this->uri->segment(n))) {
//pagination stuff
} else {
//regular behavior
}
In case you are using aplication/config/routes.php consider using _remap() instead (for this certain controller).
or use routes as following:
$route['events/(:num)'] = "events/page/$1"; //pagination "behind the scene" with method "page" (that is not seen by user).
$route['events/town/venue/(:any)'] = "events/town/venue/$1";

MVC Routing Engine routes same formatted route to different controller actions

Okay, I did my homework and search SO, and indeed I found similar questions but not reporting the behavior I'm getting.
Here is the deal, I have defined a route:
routes.MapRoute("CategoryName", "Category/Name/{text}",
new { controller = "Category", action = "Name", text = "" });
The twist here is the following:
This url: http://www.url.com/Category/Name/existingCategoryName
And this url: http://www.url.com/Category/Name/anotherExistingCategoryName
Both url's should go to the same controller method which is public ActionResult Name(string text) but sadly the first url is going to the default Index method, the second is being routed correctly.
I wonder why this happens, as I've been with .net mvc for several years and never experienced this behavior.
As a side note here are some facts:
As it's being route to different methods, I doubt the code inside them has something to do with it.
When manually write the category to something it doesn't exists in the DB as a category name it goes through the Name method.
The routes are placed correctly, as I'm aware the first route that matches the pattern will win.
Even I tried place the CategoryName route first, the behavior is the same.
When writing each link in the Category/Index I use the same #Html.RouteLink() helper, so all the links are formatted the same way.
Thanks in advance!
Are you using the - sign in the failing route?
Maybe you can find more information with the Routing debugger
And maybe you can look at this question: Failing ASP.NET MVC route. Is this a bug or corner case?
Phil Haack also give an possible answer to your problem in: ASP.NET routing: Literal sub-segment between tokens, and route values with a character from the literal sub-segment

Route all actions of a controller except one in codeigniter

I am doing a project in CodeIgniter and I want to route all the urls of a particular controller to a specific action except one. For e.g.,
I want the url
myurl/mycontroller/myaction
to be handled by the action myaction but any other urls like
myurl/mycontroller/myaction1
myurl/mycontroller/myaction2
myurl/mycontroller/myaction3
to be handled by action abc of a particular controller. I had searched across the internet and what I get is how to handle all urls by a certain controller except some. The way to do it is
$route['^(?!admin|user|setup|pages).*'] = "user/view/$0";
Here all urls will be handled by user/view except those whose 2'nd part of the url is admin, user, setup or pages.
I think routes are applied in order, so how about adding a route for the "myaction" first before the other ones?
$route['myurl/mycontroller/myaction'] = "myurl/mycontroller/myaction";
$route['myurl/mycontroller/abc'] = "myurl/mycontroller/$1";
I believe this is the correct syntax
$route['myurl/mycontroler/myaction(:any)'] = "myurl/controller_a/action";
You can verify it here
EDIT
I read your comment and I made an adjustment. Give it a go and see if it fits.
EDIT 2
Well since you just want the exact word myaction unharmed then either use (:any) or (\d+) after the word so the rerouteing happens when a number is attached to the myaction word. I haven't actually tested it yet.

Make the route parameter actually appear in the address bar

I have a tiny application in MVC 3.
In this tiny application, I want my URLs very clear and consistent.
There's just one controller with one action with one parameter.
If no value is provided (that is, / is requested by the browser), then a form is displayed to collect that single value. If a value is provided, a page is rendered.
The only route is this one:
routes.MapRoute(
"Default",
"{account}",
new { controller = "Main", action = "Index", account = UrlParameter.Optional }
);
This all works fine, but the account parameter never appears in the address line as a part of the URL. I can manually type test.com/some_account and it will work, but other than that, the account goes as a post parameter and therefore does not appear. And if I use FormMethods.Get in my form, I get ?account=whatever appended to the URL, which is also not what I want and which goes against my understanding. My understanding was that the MVC framework would try to use parameters set in the route, and only if not found, it would append them after the ?.
I've tried various flavours of setting the routes -- one route with a default parameter, or one route with a required parameter, or two routes (one with a required parameter and one without parameters); I've tried mixing HttpGet/HttpPost in all possible ways; I've tried using single action method with optional parameter string account = null and using two action methods (one with parameter, one without), but I simply can't get the thing appear in the URL.
I have also consulted the Steven Sanderson's book on MVC 3, but on the screenshots there are no parameters either (a details page for Kayak is displayed, but the URL in the address bar is htpp://localhost:XXXX/).
The only thing that definitely works and does what I want is
return RedirectToAction("Index", new { account = "whatever" });
But in order to do it, I have to first check the raw incoming URL and do not redirect if it already contains an account in it, otherwise it is an infinite loop. This seems way too strange and unnecessary.
What is the correct way to make account always appear as a part of the URL?
My understanding was that the MVC framework would try to use
parameters set in the route, and only if not found, it would append
them after the ?
Your understanding is not correct. ASP.NET MVC doesn't append anything. It's the client browser sending the form submission as defined in the HTML specification:
The method attribute of the FORM element specifies the HTTP method used
to send the form to the processing agent. This attribute may take two
values:
get: With the HTTP "get" method, the form data set is appended to the URI specified by the action attribute (with a question-mark ("?")
as separator) and this new URI is sent to the processing agent.
post: With the HTTP "post" method, the form data set is included in the body of the form and sent to the processing agent.
ASP.NET MVC routes are used to parse an incoming client HTTP request and redispatch it to the corresponding controller actions. They are also used by HTML helpers such as Html.ActionLink or Html.BeginForm to generate correct routes. It's just that for your specific scenario where you need to submit a user entered value as part of the url path (not query string) the HTML specification has nothing to offer you.
So, if you want to fight against the HTML specification you will have to use other tools: javascript. So you could use GET method and subscribe to the submit handler of the form and inside it manipulate the url so the value that was appended after the ? satisfy your requirements.
Don't think of this as ASP.NET MVC and routes and stuff. Think of it as a simple HTML page (which is what the browser sees of course) and start tackling the problem from that side. How would you in a simple HTML page achieve this?

Resources