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.
Related
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.
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.
In my MVC application, I have following route configuration,
routes.MapRoute(
name: "ProductRoute",
url: "{productName}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Now if i give some thing like this, localhost:56789/prd1/Home/Index
The first routing is working.
However if i directly access localhost:56789/Home/Index or any other controller action, like localhost:56789/Account/Login the routing is not working.
Is it because I have a {productName} defined?
For testing purpose how can I add few products like prd1, prd2 along with routing?
Routes are matched against the request in the order they are defined. When a route matching the request is found, no further routes are considered. Thus, you need to list your routes in order of decreasing specificity.
However, your first route matches more requests than your second route, i.e. it is less specific than the second route:
When ASP.NET MVC tries to match the request for Home/Index to your routes, it will match the first route because it will consider Home to be the productName, it will consider Index to be the controller name and the rest of the parameters are not required.
You need to reorder your routes or make the first route more specific. This could be done by putting constraints on the productName parameter.
UPDATE
Without knowing anything about your products and their names, it is impossible for me to suggest an appropriate constraint. Maybe you could use a numeric SKU and have a constraint like
routes.MapRoute(
name: "ProductRoute",
url: "{productName}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { productName = #"\d+" }
);
forcing productName to be numeric.
Alternatively, you could change the url to
"products/{productName}/{controller}/{action}/{id}"
For more on constraints see this link or use Google.
For route configuration
routes.MapRoute(
name: "ProductRoute",
url: "{productName}/{controller}/{action}/{id}",
defaults: new { productName = 'put your method name to get productName over here', controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "your namespace of controller" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Why do we have to specify the defaults for the default route?
This is a normal default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Why can't I just do this:
routes.MapRoute(
name: "Default",
url: "Home/Index/{id}",
defaults: new {id = UrlParameter.Optional }
);
I already specified the action and controller but when I use this way, I get an error. Does anyone know why you have to specify the action and controller in the default route?
Without a default set of parameters, how is routing supposed to know where to send this URL?
/
The defaults let you do that URL, so it knows to use the 'Home' controller's 'Index' method.
Or:
/Articles
In this case, the 'Index' action of the 'Articles' controller would be called. Without those defaults, again, routing has no way to know what to do.
Default of asp.net-mvc4 is http://domainname.com/products/1 with routes
routes.MapRoute(
name: "Products",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
and I want to rewrite to http://domainname.com/products/1.html that has .html extention .
Any ideas for this?
Do you mean this:
routes.MapRoute(
name: "Products",
url: "{controller}/{action}/{id}.html",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
Why would you want to put .html extension to a query string or route parameter?
if you take a website with html files, the extension is with the page not with the parameters. Don't know if you have any specific requirement to put it always at the end of the url. but it doesn't make any sense.
routes.MapRoute(
name: "Products",
url: "{controller}/{action}.html/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
you might have problems with iis after doing the above, please take a look at this discussion as well.
ASP.NET MVC Routing - add .html extension to routes