Show custom url? - asp.net

How to display custom url and not based on controller and action names?
My routing looks like
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "productfeature",
url: "cn/pendrive/18-gb",
defaults: new { controller = "Product", action = "Feature", id = UrlParameter.Optional }
);
}
}
and my link button looks like
<li>#Html.RouteLink("Product Feature", "productfeature")</li>
I get url as the Url mentioned in the code but i also 404 error.
http://localhost:9090/cn/pendrive/18-gb
How to go for static urls? Basically my aim is to no show controllers and action names in url.

Put your custom route before the default one:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "productfeature",
url: "cn/pendrive/18-gb",
defaults: new { controller = "Product", action = "Feature", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

Related

ASP.NET Root Url Doesn't Resolve to Default.aspx

I have an asp.net web site that won't seem to resolve to Default.aspx when initially loading. When I debug on my local machine it loads Default no problem. Unless I try to navigate to "localhost:#####/". Then it gives a 404 error. When I deploy it to a staging server, give it a virtual path "mywebapp", and load it from "mydomain.com/mywebapp" it gives a 404 as well. I have set Default.aspx to the top of the list for Default Document in IIS. If I navigate to "mydomain.com/mywebapp/default" the site loads just fine. Any suggestions? I would paste code but it is a large website and I quite honestly am not sure what I'm looking for anymore.
EDIT:
In my site I am also using DataTables for display and edit of data. In the ajax calls I was previously able to call the controller by using urls such as:
api/MyController/idvalue
but since uncovering this I had to go back and preface the urls to get them to work:
mywebapp/api/MyController/idvalue
Controller:
public class MyController : ApiController
{
[Route("api/MyContoller/{idvalue}")]
[HttpGet]
[HttpPost]
public IHttpActionResult MyControllerMethod(intidvalue)
{
}
}
WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
RouteConfig:
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { action = "Index", id = UrlParameter.Optional }
// );
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Found the solution. In my RouteConfig.cs if I comment out the line:
controller = "Home",
it works just fine.
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { action = "Index", id = UrlParameter.Optional }
// );
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
//controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
}
}
However, I'm not completely sure why this is. So if anyone is able to explain it to me that would be great!

MVC 5.0 RegisterRoutes not working

Anyone know why this is not working, i have googled it but nothing seems to work. All i want to do is allow www.abcdefg.com/username to map to a profile page but it does not work and i can't find out why.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
, new { controller = #"(admin|help|profile|Settings)" } // Constraints
);
routes.MapRoute(
"Users",
"{username}",
new { controller = "Home", action = "Index", username = "" });
}
You must adding the default route as the last route
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Users",
"{username}",
new { controller = "Home", action = "Index", username = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
, new { controller = #"(admin|help|profile|Settings)" } // Constraints
);

Url Route asp.net

my Model is
public class Organization
{
public int ID { get; set; }
public string Url { get; set; }
}
and this is my routeConfig
routes.MapRoute(
"Url",
"{controller}/{action}/{Url}",
new { controller = "Organization", action = "PageContent", Url = "" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Is there a way to fix the routing so that both Url and ID would work?
for example ...Organization/details/{ID} and ...Organization/details/{Url} would both work
As both the routes seems same you can combine both and define one and in the action (or onActionExecuting) you can decide the behavior based ID or URL
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{Segment}",
defaults: new { controller = "Home", action = "Index", Segment = UrlParameter.Optional }
);
Or you can map the route with ID only and take URL as querystring.

ASP.NET WebApi custom route

I have a simple Controller that is returning thumbnails, it is defined like:
public class ThumbnailsController : ApiController
{
public HttpResponseMessage Get(string id)
{
//code here
}
}
Everything works fine, I can access image using url http://site.com/api/Thumbnails/mylogin
But I would like to modify this method like so:
public class ThumbnailsController : ApiController
{
public HttpResponseMessage Get(string login="", int size=64)
{
//code here
}
}
Idea is to be able to call:
- http://site.com/api/Thumbnails/ - this will return current logged in user picture in default (64x64) size
- http://site.com/api/Thumbnails/mylogin - this will return mylogin user picture in default (64x64) size
- http://site.com/api/Thumbnails/mylogin/128 - this will return mylogin user picture in 128x128 size
My problem are routes, default route works with my unchanged method, but how should I change the default to get this working?
I will also have other Api Controllers but only this one should have custom route.
Here is my attempt, but it isn't working.
routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/thumbnails/{login}/{size}",
defaults: new {controller="Thumbnails", action="Get", login = RouteParameter.Optional, size = RouteParameter.Optional }
);
EDIT
This is my Controller with test method:
public class ThumbnailsController : ApiController
{
public string Get(string login="", int size=64)
{
return string.Format("login: {0}, size: {1}", login, size);
}
}
and here is my RouteConfig:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/Thumbnails/{login}/{size}",
defaults: new { controller = "Thumbnails" , login = RouteParameter.Optional, size = RouteParameter.Optional }
);
}
}
Make sure you declare your custom route before the default route in the WebApiConfig.
EDIT:
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/Thumbnails/{login}/{size}",
defaults: new { controller = "Thumbnails", action = "Get",
login = RouteParameter.Optional, size = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
The custom route should be before the default one
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "Thumbnails",
routeTemplate: "api/Thumbnails/{login}/{size}",
defaults: new { controller = "Thumbnails" , login = RouteParameter.Optional, size = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
And try to change the type of the "size" to nullable integer:
public HttpResponseMessage Get(string login = null, int? size = 64)

Add more routing to asp.net MVC Global.asax

I have one map route like :
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 = UrlParameter.Optional } // Parameter defaults
);
}
but I want to add more route URL , how can I do that ?
Just add another MapRoute()
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"SecondRoute",
"{controller}/{action}/{tags}",
new { controller = "Products", action = "Index", tags = "" }
);
I suggest you go through this excellent post on routing by The Gu.

Resources