i am tring to have links link this
/View/test
it is working only if it is int like /View/1
my view action link
#Html.ActionLink(item.className, "Viewe", "subject", new {id =item.className },null)
my control action
public ViewResult Viewe(string id)
{
//some database
return View();
}
i get this error Server Error in '/' Application.
if i change the id into anything it works fine but the link will looks like
this : View?anything=par
image of the error
route code
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
this is working for me
#Html.ActionLink(item.ClassName, "Viewe", "Subject", new { Id = item.ClassName }, null)
Related
I have a controller "Home" and 2 actions inside it called "DeliveryReturn" and one called "BasketReturn" like this:
public ActionResult DeliveryReturn(string id)
{
return View();
}
public ActionResult BasketReturn(string id)
{
return View();
}
".../Home/Return/Delivery/10" -> DeliveryReturn should be called.
".../Home/Return/Basket/10" -> BasketReturn should be called.
I tried setting up my route somewhat like this. I am really lost though and it seems like it doesn't make sense at all.
routes.MapRoute(
name: "Return",
url: "Home/Return/{type}/{id}",
);
Update:
I figured it out, that I could do something like this. However, this would require me to register two routings. How would I do it with only one?
routes.MapRoute(
name: "DeliveryReturn",
url: "Home/Return/Delivery/{id}",
defaults: new { controller = "Home", action = "DeliveryReturn", id= ""}
);
routes.MapRoute(
name: "BasketReturn",
url: "Home/Return/Basket/{id}",
defaults: new { controller = "Home", action = "BasketReturn", id= ""}
);
I am using the PagedList package to create a paging section for my tables.
Everything is working fine, but my urls are shown as:
http://localhost:55808/suppliers?page=2&sortOrder=id_desc&searchFilter=s
So I created a custom route for this using the following code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
// Index Suppliers with paging / search filter / orderby
routes.MapRoute(
"SupplierPaging",
"suppliers/{searchFilter}/{sortOrder}/{page}",
new
{
controller = "Suppliers",
action = "Index",
sortOrder = UrlParameter.Optional,
searchFilter = UrlParameter.Optional,
page = 1
}
);
// Default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Also this is working for the Index of my Suppliers controller.
Now if I go to the http://localhost:55808/suppliers/edit/1 page to edit a supplier, it goes into the Index method of my controller instead of the Edit.
Do I need to add all these methods as custom routes to get it working? Eg:
A custom route for edit
A custom route for delete
...
Or what am I missing here?
Add route constraints so the first route will not match for other actions:
routes.MapRoute(
"DefaultPaging",
"{controller}/{searchFilter}/{sortOrder}/{page}",
new
{
action = "Index",
sortOrder = UrlParameter.Optional,
searchFilter = UrlParameter.Optional,
page = 1
},
new
{
sortOrder = #"\d+",
searchFilter = #"\d+",
page = #"\d+"
}
);
Also I've replaced "suppliers" part of the route with "{controller}", so this will work for all controllers with paging.
I am a beginner in MVC 4. I have an url ../Home/NameDetailsPage/20 where controller= Home, action=NameDetailsPage and pageIndex= 20. How do i write my route engine for this url?
routes.MapRoute(
......
.....
);
In controller, NameDetailsPage works pretty fine for default page such as int page=2 :-
public ActionResult NameDetailsPage(int? page)
{
var context = new BlogContext();
IQueryable<string> list;
list = from m in context.Blogs.OrderBy(m => m.BlogId)
select m.Name;
ViewBag.total = list.ToArray().Length;
ViewBag.page = page;
var pageNumber = page ?? 1;
ViewBag.page1 = pageNumber;
return View("NameDetails", list.Skip(pageNumber * 4).Take(4));
}
But the pageNumber is always 1 whatever pageIndex in the url. So It shows same result for all the pageIndex. How can I set pageNumber other than 1. Thanks in Advance.
When you have a route like the following
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
);
The defaults object properties (controller, action, id) are what can be passed to the action (if the action has parameters that match the name).
So if you have an action:
public ActionResult NameDetailsPage(int? page)
there are no values being passed from the URL as page.
You can either change {id} to {page}, or preferably:
public ActionResult NameDetailsPage(int? id)
Based on your comment you are most of the way there. You just need to define the parameter you are passing to the action. In this case switching {id} for {page} and altering the defaults to suit.
routes.MapRoute(
name: "Page",
url: "Home/NameDetailsPage/{page}",
defaults: new { controller = "Home", action = "NameDetailsPage", page = UrlParameter.Optional } );
I have these routes:
routes.MapRoute(
"Advertisers",
"advertisers/{controller}/{action}/{id}",
new { controller = "Index", action = "Index", id = UrlParameter.Optional },
new string[] { "Portal.Areas.Advertisers.Controllers" }
);
routes.MapRoute(
"Root", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Index", action = "Index", id = UrlParameter.Optional }, // Parameter defaultsm
new string[] { "Portal.Controllers" }
);
However whenever I go to /advertisers/controller/action/id it is not reading the id parameter... what am I doing wrong?
Thanks
I'd suggest you take a look at the Route Debugger
nuget install
PM> Install-Package routedebugger
After you've installed it into your project, put this one line of code inside your application start method, and hit the url you're debugging.
protected void Application_Start()
{
RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}
This will tell you exactly why your routes aren't working as expected.
As for your actual question, is your Controller actually called IndexController? Because this doesn't seem right to me
public class IndexController : Controller
{
public ActionResult Index()
{
return View();
}
}
My assumption is that you actually have something like HomeController or AdvertiserController, and if that's the case you should have something like this
routes.MapRoute(
"advertisers_default", // Route name
"advertisers/{controller}/{action}/{id}/{advertiserName}", // URL with parameters
new { controller = "Home",
action = "Index",
advertiserName = UrlParameter.Optional },
new { id = "[0-9]+",
controller = "[a-zA-Z]+",
action = "[a-zA-Z]+" }
);
and then hit the url http://example.com/advertisers/{id}/{advertiser-name}
Simply said, this url looks wrong to me
/advertisers/controller/action/{id}
it should be
/advertisers/home/{id}
or even
/advertisers/home/{id}/{advertiser-name}
Greetings,
I have a problem with link in mvc application. When I run it via Visual Studio it's ok. The link then is as follows:
http://localhost:2566/ActivateClient/Activate/6543e2d6-707d-44ae-94eb-a75d27ea0d07
when I run it via IIS7 the link is as follows:
http://localhost/ActivationService/ActivateClient/Activate/6543e2d6-707d-44ae-94eb-a75d27ea0d07
The default route is as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
I assume I have to change this MapRoute, am I right? How to change it? ActivationService is my virtualDirectory in IIS. Can someone help me with this please?
I also tried to maproute as follows:
routes.MapRoute(
"Default", // Route name
"ActivationService/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
but also without success
Did you add the new one or replace the existing one?
If you added, you need to position it before the existing one.
routes.MapRoute(
"Default", // Route name
"ActivationService/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The rules take precedence..