Why I get error when I try to hit action method - c#

I use MVC 4 in my project.
I have an action method that I try to fire with this link:
https://localhost/Mobile/Map/Index/5/184614.560225826,666226.8163541115/7/3
The signature of the action method in Map controller is:
public ActionResult Index(int id = 0, PointF center = new PointF(), int zoom = 0, int rotation = 0)
Also, I defined this Permalink route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Permalink",
url: "Map/Index/id/zoom/rotation"
);
When I try to hit the method above, I get this error on the web page:
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
While this link works perfectly:
https://localhost/Mobile/Map/Index/5
Any idea why the first link not works?

Related

ASP.Net C# MVC create exception to a global route

I am writing a URL Shortener application.
When someone enters root.com/whatever, they are redirected to a configured URL.
I managed to create a global route which will catch the paths after the root ("whatever" above) and execute the corresponding redirection successfully.
My problem and question is this:
The admin interface is at root.com/admin and when I try to access that, I get the global controller. How do I make an exception to the global controller for "admin"?
Here is what I have in my route config right now:
routes.MapRoute(
name: "Admin",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Global",
url: "{suffix}",
defaults: new { controller = "Home", action = "Index", suffix = UrlParameter.Optional }
);
For the first route, I also tried:
routes.MapRoute(
name: "Admin",
url: "admin/{action}/{id}",
defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
And I also tried putting it second in the file.
I don't know if this will help you, but take a look at this stack overflow posting: ASP.Net MVC: Routing issue which throwing exception
This person reduced the more specific route and it worked for him.

href link not passing parameter value

I have a section of code in my view that displays an image and a URL on click of it. The URL is to a page on the site and directs it to that action method. I have the parameter as part of the URL, but in the Action method the parameter shows null.Hence I am redirected to the appropriate page but none of my functionalities inside the action method works.
Here is my code.
View:
<a class="lnkImage" href="#item.ImageURL" target="_blank"><img id="PrivateimgPreview" src="#item.ActualImage"/></a>
The value of #item.Image URL which I am getting from the database is http://localhost/TestProject/Home/DemoPage/b9f074fc-f86c-4866-a7b3-33e43025293f
And my action method in Home controller is
public ActionResult DemoPage(string blockId)
{
//Some code here
return(View);
}
This is my mapping route in Route.config
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
What am I missing?
I think that you need to either change 'blockId' to just 'id' or add the following route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{blockId}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I would think it makes more sense to just rename your parameter in the signature rather than creating a nearly identical route for this action method.

How do I force a redirect with a parameter?

I have a parameter value which I have to URL encode. Therefore my understanding is that it must be sent as a query string on the end of the URL rather than part of the main URL. I have successfully tested this by directly pasting the URL into the browser.
I am trying to redirect to a URL of:
Server/Application/Area/Controller/Action/?id=xyz
But when I use
return RedirectToAction("Action", "Controller", new { area = "Area", id = Url.Encode(uniqueId) });
I am sent to
Server/Application/Area/Controller/Action/xyz
How do I stop this happening?
It happens because of your default route is;
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Site", action = "Index", id = UrlParameter.Optional }
);
if you want to keep using this route, you should be have to change your id parameter in your url.
But if id parameter is omitted from the default route, your action redirected as you expect.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Site", action = "Index"}
);
Remove id from Default Route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
or change the name of your parameter from id to something else.
I hope this will help.

MVC 4 routing "finding" controllers inside an Area

I have a MVC 4 WebSite with several Areas... I´m using all default routing created by VS2012...
So, I can access (from Area1) :
Area1/ControllerX/ActionX
I have some controllers without Area, so I can access :
ControllerY/ActionY
All fine... But, if I try to access the ControllerX without the Area1, like that :
ControllerX/ActionX
I got that error:
Exception: The view 'ActionX' or its master was not found or no view engine supports the
searched locations. The following locations were searched: ~/Views/mangavagao/ActionX.cshtml
~/Views/Shared/ActionX.cshtml
Controller: ControllerX
Action: ActionX
I was expecting a Not Found 404 error... Why is that route been captured ?
--
Area route:
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { controller = "ControllerX", action = "ActionY", id = UrlParameter.Optional }
);
Default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "ControllerY", action = "ActionY", id = UrlParameter.Optional );
Add the namespaces parameter in the default maproute function. Then set the UseNamespaceFallback datatoken to false.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MvcApplication.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
namespaces parameter is set to prioritize the controller finding, when multiple controller with the same name exists.
MVC will still search for controller outside of this namespace, if no match is found in the namespace.
UseNamespaceFallback datatoken tells MVC to disregard the (2) statement.
hope this helps.
Try to map the area route with a namespace:
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { controller = "ControllerX", action = "ActionY", id = UrlParameter.Optional },
new[] { "App.Areas.AreaName.Controllers" }
);
Change App and AreaName to the corresponding values.
This is similar to this question: Not including area name in URL results in "The view 'Index' or its master was not found" instead of 404
In your case the namespaces need to be added to the default route, not the area route. The namespace in this case should not reference the area controllers.
Something like this should work:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
null, // object constraints
new string[] { "Namespace.Application.Controllers" } // namespaces
);

MVC 4 URL generating from Routes with Razor #Html.RouteLink

I have problems with generating URL with Html.RouteLink method using route.
My routes:
routes.MapRoute(
name: "DefaultPage",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "MessagesIndex",
url: "{controller}/{page}",
defaults: new { controller = "Messages", action = "Index", page = UrlParameter.Optional }
);
My controller(MessagesController) index method header:
public ActionResult Index(int page = 0)
And link generation:
#Html.RouteLink("Messages", "MessagesIndex")
I already tried ActionLink and Action...
EDIT: Sloved. Glimpse helped and MSDN. Its very important order of routes too.
Can you explain what problems you are having?
Maybe this will work:
#Url.Action("Index", "Messages")

Categories

Resources