route request to controller based on route parameter - asp.net-mvc-routing

I want to route request to specific version controller based on version number in URI.
for example,
routes.MapHttpRoute(
name: "APIV2",
routeTemplate: "BlogFeed/{version}/{id}",
defaults: new { id = RouteParameter.Optional, controller = "BlogFeedV2"}
);
What I want is that based on version route parameter I want to route request to that versioned controller. so if version route parameter is v2, that request should be handled by BlogFeedV2. Is there a way to handle this?

why not :
routes.MapHttpRoute(
name: "APIV2",
routeTemplate: "BlogFeed/v2/{id}",
defaults: new { id = RouteParameter.Optional, controller = "BlogFeedV2"}
);
routes.MapHttpRoute(
name: "APIV1",
routeTemplate: "BlogFeed/v1/{id}",
defaults: new { id = RouteParameter.Optional, controller = "BlogFeedV1"}
);

Related

How to prevent CDK from creating default base path mapping for new DomainName

When you create a CustomDomain while creating a RestApi OR you .addDomainName after creating the RestApi in CDK, a default base path mapping is created for the specified stage to the root of the domain.
I don't want that to be created. I want to create my own base path mapping. When I add one via domain.addBasePathMapping(), I end up with both a mapping to the root and a mapping to the specified base path. Like so:
api: example.com / stage: dev / path: (none) // don't want this
one.
api: example.com / stage: dev / path: the-base-path //want this
one.
Is there a way to either change the default base path mapping OR prevent it from being created?
Code reproduces the issue:
const apiSpec = <openapi spec loaded here>
const zone = route53.HostedZone.fromLookup(this, 'theZone', {
domainName: 'example.com'
});
//Get the existing certificate
const acmCertificate = acm.Certificate.fromCertificateArn(this, 'Certificate', CERTIFICATE_ARN);
const apiDomainName = 'example.com';
const theApi = new apigateway.SpecRestApi(this, `the-example-api`, {
deploy: true,
restApiName: 'ApiNameHere',
deployOptions: {
stageName: 'dev',
},
endpointTypes: [ apigateway.EndpointType.REGIONAL ],
apiDefinition: apigateway.ApiDefinition.fromInline(apiSpec),
endpointExportName: `endpointExportName`,
domainName: {
domainName: apiDomainName,
certificate: acmCertificate,
securityPolicy: apigateway.SecurityPolicy.TLS_1_2
}
});
const domain = theApi.domainName
domain.addBasePathMapping(theApi, {basePath: 'the-base-path', stage: theApi.deploymentStage});
//Create alias record to route to apis
const aRecord = new route53.ARecord(this, 'alias-record', {
recordName: apiDomainName,
zone,
target: route53.RecordTarget.fromAlias(new targets.ApiGateway(theApi))
});
The addDomainName method on BaseRestApi always uses the restApi object as the mapping parameter as seen here: https://github.com/aws/aws-cdk/blob/b3a7d5ba67bec09e422c0c843d7dee4653fe9aec/packages/%40aws-cdk/aws-apigateway/lib/restapi.ts#L346-L355
If you don't want that, don't provide the domainName parameter when creating the api and instead create it separate like this:
const domain = new apigateway.DomainName(theApi, 'CustomDomain', {
domainName: apiDomainName,
certificate: acmCertificate,
securityPolicy: apigateway.SecurityPolicy.TLS_1_2
});
domain.addBasePathMapping(theApi, {basePath: 'the-base-path', stage: theApi.deploymentStage});
Looks like this was a bug that someone had reported in this github issue. It has since been fixed. Now, when you create an api, you can add basePath as an option to the DomainNameOptions, preventing it from creating two separate mappings. E.g.
const theApi = new apigateway.SpecRestApi(this, `the-example-api`, {
...
...
domainName: {
domainName: apiDomainName,
certificate: acmCertificate,
securityPolicy: apigateway.SecurityPolicy.TLS_1_2,
basePath: 'the-base-path'
}
});

Specifying a RouteValue to match pattern /id

I am using asp.net mvc2 and would like my site to show details (of 123) if a user enters foo.com/123.
What is the route value I should specify for this, and in what order?
I tried
routes.MapRoute(
name: "foobar",
url: "{id}",
defaults: new { controller = "foo", action = "bar", id = UrlParameter.Optional }
);
but I get a 404.
Any help is appreciated.
routes.MapRoute( name: "foobar", url: "{id}", defaults: new { controller = "foo", action = "bar", id = UrlParameter.Optional } );
should be as follows
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "foo", action = "bar", id = ""// Parameter defaults
);

MVC2 Custom Routing Breaks when site is located in IIS sub folder

So, I have implemented custom routing in mysite.
routes.MapRoute(
"WithFriendlyNameOnly",
"{friendlyName}",
new { controller = "Home", action = "Redirect", friendlyName = String.Empty, id = UrlParameter.Optional },
new { friendlyName = new MustBeFriendlyName() }
);
routes.MapRoute(
"WithFriendlyNameDefault",
"{friendlyName}/{controller}",
new { controller = "Home", action = "Index", friendlyName = String.Empty, id = UrlParameter.Optional },
new { friendlyName = new MustBeFriendlyName() }
);
routes.MapRoute(
"WithFriendlyName",
"{friendlyName}/{controller}/{action}/{id}",
new { controller = "Home", action = "Redirect", friendlyName = String.Empty, id = UrlParameter.Optional },
new { friendlyName = new MustBeFriendlyName() }
);
routes.MapRoute(
"DefaultWithRule", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = new MustNotRequireFriendlyName() }
);
This works fine locally. I am using the friendly name to determine your context in site site. Without it, all I give you is a 404 page.
My test environment is a Windows 2008 Server (IIS 7.5), and my application is in a subfolder like this: test.mydomain.com/mysite. Everything worked fine until I did this new custom url, so I'm fairly sure it isn't IIS or the server. However, my custom routes are not working at all. My regular routes are working just fine - like the home page, some information pages etc. It is only the pages with my custom routing. What did I do wrong?
Have you tried to switch routes order making more specific at the beginning? It worked for me.
Try also that debugger: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
Helps very much!

How can I change the ActionLink behavior?

Been new to MVC 2, how can we get a link as:
http://localhost:13269/Terms
instead
http://localhost:13269/Frontend/Terms
as this is the result of:
<%: Html.ActionLink("Terms & Conditions", "Terms", "Frontpage")%>
even if I don't specify the Controller like <%: Html.ActionLink("Terms & Conditions", "Terms")%>
as I changed the route to
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Frontend", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Try this as a new route definition (declare this before the default route)
routes.MapRoute(
"DefaultFontEnd", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Frontend", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

ActionLink in Asp.Net Mvc 2 does not reidrect

I have the following default and only route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
In my Site.Master I have the following:
<%= Html.ActionLink("Profile", "Details", "Users")%>
If I am on the following Url:
http://localhost:1155/Users/Details/1
and I click the link above it goes the same page.
Should it not go to the following url?
http://localhost:1155/Users/Details
For some reason it is keeping the id in the Url.
For some reason it is keeping the id in the Url.
It is by design.
Try this to get rid of id:
<%= Html.ActionLink("Profile", "Details", "Users", new { id = "" }, null)%>