I have a ProdcutsController where i have 2 Action methods. Index and Details. Index will return list of products and Details will return details of a selected product id.
So my urls are like
sitename/Products/
will load index view to show a list of products.
sitename/Products/Details/1234
will load details view to show the details of product 1234.
Now i want to avoid the "Details" word from my second url. so that it should look like
sitename/Products/1234
I tried to rename my action method from "Details" to "Index" with a parameter in it. But it showed me the error "Method is is ambiguous
"
I tried this
public ActionResult Index()
{
//code to load Listing view
}
public ActionResult Index(string? id)
{
//code to load details view
}
I am getting this error now
The type 'string' must be a non-nullable value type in order to use
it as parameter 'T' in the generic type or method 'System.Nullable<T>
Realized that it does not support method overloading ! How do i handle this ? should i update my route definition ?
Use this:
Assuming the value is an integer type.
This is another option:
A
string
is a reference type so anull
can already be assigned to it without needing aNullable<T>
.